<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Python 3 备忘清单
 &#x26;  python cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="Python 3Python 备忘单是 Python 3 编程语言的单页参考表

入门，为开发人员分享快速参考备忘单。">
<meta keywords="python,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/python.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.5"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="python-3-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 448 512" height="1em" width="1em">
  <path d="M439.8 200.5c-7.7-30.9-22.3-54.2-53.4-54.2h-40.1v47.4c0 36.8-31.2 67.8-66.8 67.8H172.7c-29.2 0-53.4 25-53.4 54.3v101.8c0 29 25.2 46 53.4 54.3 33.8 9.9 66.3 11.7 106.8 0 26.9-7.8 53.4-23.5 53.4-54.3v-40.7H226.2v-13.6h160.2c31.1 0 42.6-21.7 53.4-54.2 11.2-33.5 10.7-65.7 0-108.6zM286.2 404c11.1 0 20.1 9.1 20.1 20.3 0 11.3-9 20.4-20.1 20.4-11 0-20.1-9.2-20.1-20.4.1-11.3 9.1-20.3 20.1-20.3zM167.8 248.1h106.8c29.7 0 53.4-24.5 53.4-54.3V91.9c0-29-24.4-50.7-53.4-55.6-35.8-5.9-74.7-5.6-106.8.1-45.2 8-53.4 24.7-53.4 55.6v40.7h106.9v13.6h-147c-31.1 0-58.3 18.7-66.8 54.2-9.8 40.7-10.2 66.1 0 108.6 7.6 31.6 25.7 54.2 56.8 54.2H101v-48.8c0-35.3 30.5-66.4 66.8-66.4zm-6.7-142.6c-11.1 0-20.1-9.1-20.1-20.3.1-11.3 9-20.4 20.1-20.4 11 0 20.1 9.2 20.1 20.4s-9 20.3-20.1 20.3z"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#python-3-备忘清单"><span class="icon icon-link"></span></a>Python 3 备忘清单</h1><div class="wrap-body">
<p>Python 备忘单是 <a href="https://www.python.org/">Python 3</a> 编程语言的单页参考表</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#控制台打印">控制台打印</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量">变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#内置数据类型">内置数据类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串切片">字符串切片</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列表">列表</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#判断">判断</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#循环">循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#函数">函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#文件处理">文件处理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#算术">算术</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#加等于">加等于</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#f-字符串-python-36">f-字符串 (Python 3.6+)</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-数据类型">Python 数据类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串">字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数值">数值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#布尔值">布尔值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列表-1">列表</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#元组">元组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#集合">集合</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字典">字典</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类型转换">类型转换</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#转换为整数">转换为整数</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#转换为浮点数">转换为浮点数</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#转换为字符串">转换为字符串</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-字符串">Python 字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#下标访问">下标访问</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#循环-1">循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#切割字符串">切割字符串</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#步长">步长</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#获取长度">获取长度</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#重复多次">重复多次</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#存在性判断">存在性判断</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串拼接">字符串拼接</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#格式化">格式化</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#format-方法">format() 方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#转义符号">转义符号</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#控制台输入">控制台输入</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#头尾判断">头尾判断</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#插入分隔符拼接">插入分隔符拼接</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-f-字符串-python-36">Python f-字符串 (Python 3.6+)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#f-字符串-用法">f-字符串 用法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#填充对齐">填充对齐</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#按类型输出">按类型输出</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#显示正负号">显示正负号</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#其它">其它</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-列表">Python 列表</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义">定义</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#生成">生成</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#添加元素">添加元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#切片">切片</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#切片-1">切片</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#省略索引">省略索引</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#间隔索引">间隔索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除">删除</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列表边界">列表边界</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#连接">连接</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#排序和反转">排序和反转</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#计数">计数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#重复">重复</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#搜索">搜索</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-判断">Python 判断</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#if-else">if-else</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#if-elif-else">if-elif-else</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#三目运算">三目运算</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-循环">Python 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#一般形式">一般形式</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#带索引">带索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#while-循环">while 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#跳出循环">跳出循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#跳过一轮循环">跳过一轮循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#范围循环">范围循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用-zip">使用 zip()</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#列表生成式">列表生成式</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-函数">Python 函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基础">基础</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#返回">返回</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#位置参数">位置参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#关键字参数">关键字参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#返回多个">返回多个</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#默认值">默认值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#匿名函数">匿名函数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-解包">Python 解包</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#等量解包">等量解包</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#适量解包">适量解包</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#过量解包">过量解包</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#解包取左边">解包取左边</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#解包取两边">解包取两边</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#解包取右边">解包取右边</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#解包集合">解包集合</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#解包迭代器">解包迭代器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#解包字典">解包字典</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#生成式中的解包">生成式中的解包</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#迭代中解包">迭代中解包</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#函数中的解包">函数中的解包</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-模块">Python 模块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#导入模块">导入模块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从一个模块导入">从一个模块导入</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#导入一个模块的全部">导入一个模块的全部</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#给模块起别名">给模块起别名</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#浏览模块的函数和属性">浏览模块的函数和属性</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-文件处理">Python 文件处理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#读取文件">读取文件</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#逐行">逐行</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#带行号">带行号</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串-1">字符串</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#写入一个字符串">写入一个字符串</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#读取一个字符串">读取一个字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#对象">对象</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#写一个对象">写一个对象</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#读取对象">读取对象</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除文件">删除文件</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查和删除">检查和删除</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除文件夹">删除文件夹</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-类和继承">Python 类和继承</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义-1">定义</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#构造函数">构造函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#方法">方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类变量">类变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#super-函数">super() 函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#repr-方法">repr() 方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#用户定义的异常">用户定义的异常</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多态性">多态性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#重写">重写</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#继承">继承</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-数据模型">Python 数据模型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#自定义类创建">自定义类创建</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#上下文管理器">上下文管理器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#特殊方法">特殊方法</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#python-类型标注-python-35">Python 类型标注 (Python 3.5+)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量-1">变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量-2">变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#参数">参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#位置参数-1">位置参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#返回值">返回值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多种可能的返回值">多种可能的返回值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#关键字参数-1">关键字参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多个返回值">多个返回值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多种可能的返回值-310">多种可能的返回值 (3.10+)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#属性">属性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#标注自己">标注自己</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#标注自己-311">标注自己 (3.11+)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#标注一个值为类型的参数">标注一个值为类型的参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#标注一个值为函数的参数">标注一个值为函数的参数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#各种各样的">各种各样的</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#注释">注释</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#生成器">生成器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#要列出的生成器">要列出的生成器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#处理异常">处理异常</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#pyenv--pipenv">pyenv &#x26; pipenv</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#pyenv">pyenv</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#pipenv">pipenv</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="介绍"><a aria-hidden="true" tabindex="-1" href="#介绍"><span class="icon icon-link"></span></a>介绍</h3><div class="wrap-body">
<ul>
<li><a href="https://www.python.org/">Python 官方网站</a>  <em>(python.org)</em></li>
<li><a href="https://docs.python.org/zh-cn/3/index.html">Python 文档</a>  <em>(docs.python.org)</em></li>
<li><a href="https://learnxinyminutes.com/docs/zh-cn/python-cn/">Y 分钟学会 Python</a> <em>(learnxinyminutes.com)</em></li>
<li><a href="./regex.html#python-%E4%B8%AD%E7%9A%84%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F">Python 中的正则表达式</a> <em>(jaywcjlove.github.io)</em></li>
</ul>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="控制台打印"><a aria-hidden="true" tabindex="-1" href="#控制台打印"><span class="icon icon-link"></span></a>控制台打印</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Hello, World!"</span><span class="token punctuation">)</span>
</span><span class="code-line">Hello<span class="token punctuation">,</span> World!
</span></code></pre>
<p>著名的“Hello World”程序在 Python 中的实现</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="变量"><a aria-hidden="true" tabindex="-1" href="#变量"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">age <span class="token operator">=</span> <span class="token number">18</span>       <span class="token comment"># 年龄是 int 类型</span>
</span><span class="code-line">name <span class="token operator">=</span> <span class="token string">"John"</span>  <span class="token comment"># 名字现在是 str 类型</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span>
</span></code></pre>
<ul>
<li>Python 不能在没有赋值的情况下声明变量</li>
<li>变量可以存放不同类型的值</li>
</ul>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="内置数据类型"><a aria-hidden="true" tabindex="-1" href="#内置数据类型"><span class="icon icon-link"></span></a>内置数据类型</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>序列指一批有序的元素，集合指一批无序且不重复的元素</p>





































<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>str</code></td><td align="left">文本／字符串（Text）</td></tr><tr><td align="left"><code>int</code>, <code>float</code>, <code>complex</code></td><td align="left">数值（Numeric）</td></tr><tr><td align="left"><code>dict</code></td><td align="left">映射／键值对（Mapping）</td></tr><tr><td align="left"><code>list</code>, <code>tuple</code>, <code>range</code></td><td align="left">序列（Sequence）</td></tr><tr><td align="left"><code>set</code>, <code>frozenset</code></td><td align="left">集合（Set）</td></tr><tr><td align="left"><code>bool</code></td><td align="left">布尔值／逻辑值（Boolean）</td></tr><tr><td align="left"><code>bytes</code>, <code>bytearray</code>, <br> <code>memoryview</code></td><td align="left">二进制数据（Binary）</td></tr></tbody></table>
<p>查看: <a href="#python-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B">数据类型</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串切片"><a aria-hidden="true" tabindex="-1" href="#字符串切片"><span class="icon icon-link"></span></a>字符串切片</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> msg <span class="token operator">=</span> <span class="token string">"Hello, World!"</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">print</span><span class="token punctuation">(</span>msg<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">llo
</span></code></pre>
<p>查看: <a href="#python-%E5%AD%97%E7%AC%A6%E4%B8%B2">字符串</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列表"><a aria-hidden="true" tabindex="-1" href="#列表"><span class="icon icon-link"></span></a>列表</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">mylist <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
</span><span class="code-line">mylist<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line">mylist<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">for</span> item <span class="token keyword">in</span> mylist<span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token comment"># 打印输出 1,2</span>
</span></code></pre>
<p>查看: <a href="#python-%E5%88%97%E8%A1%A8">列表</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="判断"><a aria-hidden="true" tabindex="-1" href="#判断"><span class="icon icon-link"></span></a>判断</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">num <span class="token operator">=</span> <span class="token number">200</span>
</span><span class="code-line"><span class="token keyword">if</span> num <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"num is greater than 0"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">else</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"num is not greater than 0"</span><span class="token punctuation">)</span>
</span></code></pre>
<p>查看: <a href="#python-%E5%88%A4%E6%96%AD">判断</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="循环"><a aria-hidden="true" tabindex="-1" href="#循环"><span class="icon icon-link"></span></a>循环</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">for</span> item <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">if</span> item <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">:</span> <span class="token keyword">break</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">else</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Finally finished!"</span><span class="token punctuation">)</span>
</span></code></pre>
<p>查看: <a href="#python-%E5%BE%AA%E7%8E%AF">循环</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="函数"><a aria-hidden="true" tabindex="-1" href="#函数"><span class="icon icon-link"></span></a>函数</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">def</span> <span class="token function">my_function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>     <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"来自函数的你好"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> my_function<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">来自函数的你好
</span></code></pre>
<p>查看: <a href="#%E5%87%BD%E6%95%B0">函数</a></p>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="文件处理"><a aria-hidden="true" tabindex="-1" href="#文件处理"><span class="icon icon-link"></span></a>文件处理</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"myfile.txt"</span><span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'utf8'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token builtin">file</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">for</span> line <span class="token keyword">in</span> <span class="token builtin">file</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span>
</span></code></pre>
<p>查看: <a href="#python-%E6%96%87%E4%BB%B6%E5%A4%84%E7%90%86">文件处理</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="算术"><a aria-hidden="true" tabindex="-1" href="#算术"><span class="icon icon-link"></span></a>算术</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">result <span class="token operator">=</span> <span class="token number">10</span> <span class="token operator">+</span> <span class="token number">30</span> <span class="token comment"># => 40</span>
</span><span class="code-line">result <span class="token operator">=</span> <span class="token number">40</span> <span class="token operator">-</span> <span class="token number">10</span> <span class="token comment"># => 30</span>
</span><span class="code-line">result <span class="token operator">=</span> <span class="token number">50</span> <span class="token operator">*</span> <span class="token number">5</span>  <span class="token comment"># => 250</span>
</span><span class="code-line">result <span class="token operator">=</span> <span class="token number">16</span> <span class="token operator">/</span> <span class="token number">4</span>  <span class="token comment"># => 4.0 (Float Division)</span>
</span><span class="code-line">result <span class="token operator">=</span> <span class="token number">16</span> <span class="token operator">//</span> <span class="token number">4</span> <span class="token comment"># => 4 (Integer Division)</span>
</span><span class="code-line">result <span class="token operator">=</span> <span class="token number">25</span> <span class="token operator">%</span> <span class="token number">2</span>  <span class="token comment"># => 1</span>
</span><span class="code-line">result <span class="token operator">=</span> <span class="token number">5</span> <span class="token operator">**</span> <span class="token number">3</span>  <span class="token comment"># => 125</span>
</span></code></pre>
<p><code>/</code> 表示 x 和 y 的商，<code>//</code> 表示 x 和 y 的底商，另见 <a href="https://stackoverflow.com/a/183870/13192320">StackOverflow</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="加等于"><a aria-hidden="true" tabindex="-1" href="#加等于"><span class="icon icon-link"></span></a>加等于</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">counter <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">counter <span class="token operator">+=</span> <span class="token number">10</span>           <span class="token comment"># => 10</span>
</span><span class="code-line">counter <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">counter <span class="token operator">=</span> counter <span class="token operator">+</span> <span class="token number">10</span>  <span class="token comment"># => 10</span>
</span><span class="code-line">message <span class="token operator">=</span> <span class="token string">"Part 1."</span>
</span><span class="code-line"><span class="token comment"># => Part 1.Part 2.</span>
</span><span class="code-line">message <span class="token operator">+=</span> <span class="token string">"Part 2."</span>   
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="f-字符串-python-36"><a aria-hidden="true" tabindex="-1" href="#f-字符串-python-36"><span class="icon icon-link"></span></a>f-字符串 (Python 3.6+)</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> website <span class="token operator">=</span> <span class="token string">'Quick Reference'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f"Hello, </span><span class="token interpolation"><span class="token punctuation">{</span>website<span class="token punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token string">"Hello, Quick Reference"</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> num <span class="token operator">=</span> <span class="token number">10</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span>num<span class="token punctuation">}</span></span><span class="token string"> + 10 = </span><span class="token interpolation"><span class="token punctuation">{</span>num <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'10 + 10 = 20'</span>
</span></code></pre>
<p>查看: <a href="#python-f-%E5%AD%97%E7%AC%A6%E4%B8%B2-python-36">f-字符串</a></p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-数据类型"><a aria-hidden="true" tabindex="-1" href="#python-数据类型"><span class="icon icon-link"></span></a>Python 数据类型</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串"><a aria-hidden="true" tabindex="-1" href="#字符串"><span class="icon icon-link"></span></a>字符串</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">hello <span class="token operator">=</span> <span class="token string">"Hello World"</span>
</span><span class="code-line">hello <span class="token operator">=</span> <span class="token string">'Hello World'</span>
</span><span class="code-line">multi_string <span class="token operator">=</span> <span class="token triple-quoted-string string">"""Multiline Strings
</span></span><span class="code-line"><span class="token triple-quoted-string string">Lorem ipsum dolor sit amet,
</span></span><span class="code-line"><span class="token triple-quoted-string string">consectetur adipiscing elit """</span>
</span></code></pre>
<p>查看: <a href="#python-%E5%AD%97%E7%AC%A6%E4%B8%B2">字符串</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数值"><a aria-hidden="true" tabindex="-1" href="#数值"><span class="icon icon-link"></span></a>数值</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">x <span class="token operator">=</span> <span class="token number">1</span>    <span class="token comment"># 整数</span>
</span><span class="code-line">y <span class="token operator">=</span> <span class="token number">2.8</span>  <span class="token comment"># 浮点小数</span>
</span><span class="code-line">z <span class="token operator">=</span> <span class="token number">1j</span>   <span class="token comment"># 复数</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">&#x3C;</span><span class="token keyword">class</span> <span class="token string">'int'</span><span class="token operator">></span>
</span></code></pre>
<p>只要内存足够，可以容纳无限大(小)的数值</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="布尔值"><a aria-hidden="true" tabindex="-1" href="#布尔值"><span class="icon icon-link"></span></a>布尔值</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">my_bool <span class="token operator">=</span> <span class="token boolean">True</span> 
</span><span class="code-line">my_bool <span class="token operator">=</span> <span class="token boolean">False</span>
</span><span class="code-line"><span class="token builtin">bool</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>     <span class="token comment"># => False</span>
</span><span class="code-line"><span class="token builtin">bool</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>     <span class="token comment"># => True</span>
</span></code></pre>
<p>bool 是 int 的子类</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列表-1"><a aria-hidden="true" tabindex="-1" href="#列表-1"><span class="icon icon-link"></span></a>列表</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">list1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"apple"</span><span class="token punctuation">,</span> <span class="token string">"banana"</span><span class="token punctuation">,</span> <span class="token string">"cherry"</span><span class="token punctuation">]</span>
</span><span class="code-line">list2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">,</span> <span class="token boolean">False</span><span class="token punctuation">]</span>
</span><span class="code-line">list3 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>
</span><span class="code-line">list4 <span class="token operator">=</span> <span class="token builtin">list</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
<p>查看: <a href="#python-%E5%88%97%E8%A1%A8">列表</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="元组"><a aria-hidden="true" tabindex="-1" href="#元组"><span class="icon icon-link"></span></a>元组</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">my_tuple <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line">my_tuple <span class="token operator">=</span> <span class="token builtin">tuple</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
<p>类似列表，但自身不可变</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="集合"><a aria-hidden="true" tabindex="-1" href="#集合"><span class="icon icon-link"></span></a>集合</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">set1 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token string">"b"</span><span class="token punctuation">,</span> <span class="token string">"c"</span><span class="token punctuation">}</span>   
</span><span class="code-line">set2 <span class="token operator">=</span> <span class="token builtin">set</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token string">"b"</span><span class="token punctuation">,</span> <span class="token string">"c"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
<p>类似列表，但里面的元素是无序而不重复的</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字典"><a aria-hidden="true" tabindex="-1" href="#字典"><span class="icon icon-link"></span></a>字典</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> empty_dict <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"one"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"two"</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">"three"</span><span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token string">"one"</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token number">1</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">.</span>keys<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">dict_keys<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'one'</span><span class="token punctuation">,</span> <span class="token string">'two'</span><span class="token punctuation">,</span> <span class="token string">'three'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">.</span>values<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">dict_values<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">.</span>update<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">"four"</span><span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">.</span>keys<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">dict_keys<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'one'</span><span class="token punctuation">,</span> <span class="token string">'two'</span><span class="token punctuation">,</span> <span class="token string">'three'</span><span class="token punctuation">,</span> <span class="token string">'four'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token string">'four'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token number">4</span>
</span></code></pre>
<p>键-值对，一种像 JSON 那样对象</p>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="类型转换"><a aria-hidden="true" tabindex="-1" href="#类型转换"><span class="icon icon-link"></span></a>类型转换</h3><div class="wrap-body">
<h4 id="转换为整数"><a aria-hidden="true" tabindex="-1" href="#转换为整数"><span class="icon icon-link"></span></a>转换为整数</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">x <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>       <span class="token comment"># 得到 1</span>
</span><span class="code-line">y <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token number">2.8</span><span class="token punctuation">)</span>     <span class="token comment"># 得到 2</span>
</span><span class="code-line">z <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token string">"3"</span><span class="token punctuation">)</span>     <span class="token comment"># 得到 3</span>
</span></code></pre>
<h4 id="转换为浮点数"><a aria-hidden="true" tabindex="-1" href="#转换为浮点数"><span class="icon icon-link"></span></a>转换为浮点数</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">x <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>     <span class="token comment"># 得到 1.0</span>
</span><span class="code-line">y <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token number">2.8</span><span class="token punctuation">)</span>   <span class="token comment"># 得到 2.8</span>
</span><span class="code-line">z <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token string">"3"</span><span class="token punctuation">)</span>   <span class="token comment"># 得到 3.0</span>
</span><span class="code-line">w <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span><span class="token string">"4.2"</span><span class="token punctuation">)</span> <span class="token comment"># 得到 4.2</span>
</span></code></pre>
<h4 id="转换为字符串"><a aria-hidden="true" tabindex="-1" href="#转换为字符串"><span class="icon icon-link"></span></a>转换为字符串</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">x <span class="token operator">=</span> <span class="token builtin">str</span><span class="token punctuation">(</span><span class="token string">"s1"</span><span class="token punctuation">)</span>    <span class="token comment"># 得到 "s1"</span>
</span><span class="code-line">y <span class="token operator">=</span> <span class="token builtin">str</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>       <span class="token comment"># 得到 "2"</span>
</span><span class="code-line">z <span class="token operator">=</span> <span class="token builtin">str</span><span class="token punctuation">(</span><span class="token number">3.0</span><span class="token punctuation">)</span>     <span class="token comment"># 得到 "3.0"</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-字符串"><a aria-hidden="true" tabindex="-1" href="#python-字符串"><span class="icon icon-link"></span></a>Python 字符串</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="下标访问"><a aria-hidden="true" tabindex="-1" href="#下标访问"><span class="icon icon-link"></span></a>下标访问</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> hello <span class="token operator">=</span> <span class="token string">"Hello, World"</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">print</span><span class="token punctuation">(</span>hello<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 获取第二个字符</span>
</span><span class="code-line">e
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">print</span><span class="token punctuation">(</span>hello<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 获取倒数第一个字符</span>
</span><span class="code-line">d
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>hello<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 得到的还是字符串</span>
</span><span class="code-line"><span class="token operator">&#x3C;</span><span class="token keyword">class</span> <span class="token string">'str'</span><span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="循环-1"><a aria-hidden="true" tabindex="-1" href="#循环-1"><span class="icon icon-link"></span></a>循环</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">for</span> char <span class="token keyword">in</span> <span class="token string">"foo"</span><span class="token punctuation">:</span>
</span><span class="code-line"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>     <span class="token keyword">print</span><span class="token punctuation">(</span>char<span class="token punctuation">)</span>
</span><span class="code-line">f
</span><span class="code-line">o
</span><span class="code-line">o
</span></code></pre>
<p>对字符串 for-in 可以得到每个字符（类型还是字符串）</p>
</div></div></div><div class="wrap h3body-exist row-span-4"><div class="wrap-header h3wrap"><h3 id="切割字符串"><a aria-hidden="true" tabindex="-1" href="#切割字符串"><span class="icon icon-link"></span></a>切割字符串</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-4-->
<pre class="language-java"><code class="language-java code-highlight"><span class="code-line"> ┌───┬───┬───┬───┬───┬───┬───┐
</span><span class="code-line"> <span class="token operator">|</span> m <span class="token operator">|</span> y <span class="token operator">|</span> b <span class="token operator">|</span> a <span class="token operator">|</span> c <span class="token operator">|</span> o <span class="token operator">|</span> n <span class="token operator">|</span>
</span><span class="code-line"> └───┴───┴───┴───┴───┴───┴───┘
</span><span class="code-line"> <span class="token number">0</span>   <span class="token number">1</span>   <span class="token number">2</span>   <span class="token number">3</span>   <span class="token number">4</span>   <span class="token number">5</span>   <span class="token number">6</span>   <span class="token number">7</span>
</span><span class="code-line"><span class="token operator">-</span><span class="token number">7</span>  <span class="token operator">-</span><span class="token number">6</span>  <span class="token operator">-</span><span class="token number">5</span>  <span class="token operator">-</span><span class="token number">4</span>  <span class="token operator">-</span><span class="token number">3</span>  <span class="token operator">-</span><span class="token number">2</span>  <span class="token operator">-</span><span class="token number">1</span>
</span></code></pre>
<hr>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token operator">=</span> <span class="token string">'mybacon'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'bac'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'my'</span>
</span></code></pre>
<hr>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token operator">=</span> <span class="token string">'mybacon'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'my'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'bacon'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span> s<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'mybacon'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'mybacon'</span>
</span></code></pre>
<hr>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token operator">=</span> <span class="token string">'mybacon'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'baco'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token number">6</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'baco'</span>
</span></code></pre>
<h4 id="步长"><a aria-hidden="true" tabindex="-1" href="#步长"><span class="icon icon-link"></span></a>步长</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token operator">=</span> <span class="token string">'12345'</span> <span class="token operator">*</span> <span class="token number">5</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s
</span><span class="code-line"><span class="token string">'1234512345123451234512345'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'11111'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'55555'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'55555'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'5432154321543215432154321'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="获取长度"><a aria-hidden="true" tabindex="-1" href="#获取长度"><span class="icon icon-link"></span></a>获取长度</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> hello <span class="token operator">=</span> <span class="token string">"Hello, World!"</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>hello<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token number">13</span>
</span></code></pre>
<p><code>len()</code> 函数返回字符串的长度</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="重复多次"><a aria-hidden="true" tabindex="-1" href="#重复多次"><span class="icon icon-link"></span></a>重复多次</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token operator">=</span> <span class="token string">'===+'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> n <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token operator">*</span> n
</span><span class="code-line"><span class="token string">'===+===+===+===+===+===+===+===+'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="存在性判断"><a aria-hidden="true" tabindex="-1" href="#存在性判断"><span class="icon icon-link"></span></a>存在性判断</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token operator">=</span> <span class="token string">'spam'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token keyword">in</span> <span class="token string">'I saw spamalot!'</span>
</span><span class="code-line"><span class="token boolean">True</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token keyword">not</span> <span class="token keyword">in</span> <span class="token string">'I saw The Holy Grail!'</span>
</span><span class="code-line"><span class="token boolean">True</span>
</span></code></pre>
<p>判断 "spam" 这个字符串是否在其它字符串里</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串拼接"><a aria-hidden="true" tabindex="-1" href="#字符串拼接"><span class="icon icon-link"></span></a>字符串拼接</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token operator">=</span> <span class="token string">'spam'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> t <span class="token operator">=</span> <span class="token string">'egg'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> s <span class="token operator">+</span> t  <span class="token comment"># 可以使用加号进行拼接</span>
</span><span class="code-line"><span class="token string">'spamegg'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string">'spam'</span> <span class="token string">'egg'</span>  <span class="token comment"># 两个字符串之间可以省略加号</span>
</span><span class="code-line"><span class="token string">'spamegg'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="格式化"><a aria-hidden="true" tabindex="-1" href="#格式化"><span class="icon icon-link"></span></a>格式化</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">name <span class="token operator">=</span> <span class="token string">"John"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Hello, %s!"</span> <span class="token operator">%</span> name<span class="token punctuation">)</span>
</span></code></pre>
<hr>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">name <span class="token operator">=</span> <span class="token string">"John"</span>
</span><span class="code-line">age <span class="token operator">=</span> <span class="token number">23</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"%s is %d years old."</span> <span class="token operator">%</span> <span class="token punctuation">(</span>name<span class="token punctuation">,</span> age<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
<h4 id="format-方法"><a aria-hidden="true" tabindex="-1" href="#format-方法"><span class="icon icon-link"></span></a>format() 方法</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">txt1 <span class="token operator">=</span> <span class="token string">"My name is {fname}, I'm {age}"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>fname<span class="token operator">=</span><span class="token string">"John"</span><span class="token punctuation">,</span> age<span class="token operator">=</span><span class="token number">36</span><span class="token punctuation">)</span>
</span><span class="code-line">txt2 <span class="token operator">=</span> <span class="token string">"My name is {0}, I'm {1}"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">"John"</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">)</span>
</span><span class="code-line">txt3 <span class="token operator">=</span> <span class="token string">"My name is {}, I'm {}"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token string">"John"</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="转义符号"><a aria-hidden="true" tabindex="-1" href="#转义符号"><span class="icon icon-link"></span></a>转义符号</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->





































<table><thead><tr><th>转义符号</th><th>对应的操作</th></tr></thead><tbody><tr><td><code>\\</code></td><td>输出反斜杠</td></tr><tr><td><code>\'</code></td><td>输出单引号</td></tr><tr><td><code>\"</code></td><td>输出双引号</td></tr><tr><td><code>\n</code></td><td>换行</td></tr><tr><td><code>\t</code></td><td>水平制表符</td></tr><tr><td><code>\r</code></td><td>光标回到首位</td></tr><tr><td><code>\b</code></td><td>退格</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="控制台输入"><a aria-hidden="true" tabindex="-1" href="#控制台输入"><span class="icon icon-link"></span></a>控制台输入</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> name <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"Enter your name: "</span><span class="token punctuation">)</span>
</span><span class="code-line">Enter your name<span class="token punctuation">:</span> Tom
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> name
</span><span class="code-line"><span class="token string">'Tom'</span>
</span></code></pre>
<p>从控制台获取输入数据</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="头尾判断"><a aria-hidden="true" tabindex="-1" href="#头尾判断"><span class="icon icon-link"></span></a>头尾判断</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># 是否以 H 开头</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string">"Hello, world!"</span><span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">"H"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token boolean">True</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># 是否以 h 开头</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string">"Hello, world!"</span><span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">"h"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token boolean">False</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token comment"># 是否以 ! 结尾</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string">"Hello, world!"</span><span class="token punctuation">.</span>endswith<span class="token punctuation">(</span><span class="token string">"!"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token boolean">True</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="插入分隔符拼接"><a aria-hidden="true" tabindex="-1" href="#插入分隔符拼接"><span class="icon icon-link"></span></a>插入分隔符拼接</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string">"、"</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">"John"</span><span class="token punctuation">,</span> <span class="token string">"Peter"</span><span class="token punctuation">,</span> <span class="token string">"Vicky"</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token string">'John、Peter、Vicky'</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-f-字符串-python-36"><a aria-hidden="true" tabindex="-1" href="#python-f-字符串-python-36"><span class="icon icon-link"></span></a>Python f-字符串 (Python 3.6+)</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="f-字符串-用法"><a aria-hidden="true" tabindex="-1" href="#f-字符串-用法"><span class="icon icon-link"></span></a>f-字符串 用法</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> website <span class="token operator">=</span> <span class="token string">'Reference'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f"Hello, </span><span class="token interpolation"><span class="token punctuation">{</span>website<span class="token punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token string">"Hello, Reference"</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> num <span class="token operator">=</span> <span class="token number">10</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span>num<span class="token punctuation">}</span></span><span class="token string"> + 10 = </span><span class="token interpolation"><span class="token punctuation">{</span>num <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'10 + 10 = 20'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f"""He said </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token string">"I'm John"</span><span class="token punctuation">}</span></span><span class="token string">"""</span></span>
</span><span class="code-line"><span class="token string">"He said I'm John"</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'5 </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token string">"{stars}"</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'5 {stars}'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'{{5}} </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token string">"stars"</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'{5} stars'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> name <span class="token operator">=</span> <span class="token string">'Eric'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> age <span class="token operator">=</span> <span class="token number">27</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f"""Hello!
</span></span></span><span class="code-line"><span class="token string-interpolation"><span class="token string">...     I'm </span><span class="token interpolation"><span class="token punctuation">{</span>name<span class="token punctuation">}</span></span><span class="token string">.
</span></span></span><span class="code-line"><span class="token string-interpolation"><span class="token string">...     I'm </span><span class="token interpolation"><span class="token punctuation">{</span>age<span class="token punctuation">}</span></span><span class="token string">."""</span></span>
</span><span class="code-line"><span class="token string">"Hello!\n    I'm Eric.\n    I'm 27."</span>
</span></code></pre>
<p>它从 Python 3.6 开始可用，另见: <a href="https://docs.python.org/zh-cn/3/reference/lexical_analysis.html#f-strings">格式字符串字面值</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="填充对齐"><a aria-hidden="true" tabindex="-1" href="#填充对齐"><span class="icon icon-link"></span></a>填充对齐</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token string">"text"</span><span class="token punctuation">:</span><span class="token format-spec">10</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>   <span class="token comment"># 使用空格填充到指定长度</span>
</span><span class="code-line"><span class="token string">'text      '</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token string">"test"</span><span class="token punctuation">:</span><span class="token format-spec">*>10</span><span class="token punctuation">}</span></span><span class="token string">'</span></span> <span class="token comment"># 向左填充</span>
</span><span class="code-line"><span class="token string">'******test'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token string">"test"</span><span class="token punctuation">:</span><span class="token format-spec">*&#x3C;10</span><span class="token punctuation">}</span></span><span class="token string">'</span></span> <span class="token comment"># 向右填充</span>
</span><span class="code-line"><span class="token string">'test******'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token string">"test"</span><span class="token punctuation">:</span><span class="token format-spec">*^10</span><span class="token punctuation">}</span></span><span class="token string">'</span></span> <span class="token comment"># 居中填充</span>
</span><span class="code-line"><span class="token string">'***test***'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">12345</span><span class="token punctuation">:</span><span class="token format-spec">0>10</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>  <span class="token comment"># 使用数字填充</span>
</span><span class="code-line"><span class="token string">'0000012345'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="按类型输出"><a aria-hidden="true" tabindex="-1" href="#按类型输出"><span class="icon icon-link"></span></a>按类型输出</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">:</span><span class="token format-spec">b</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>     <span class="token comment"># 输出二进制数值</span>
</span><span class="code-line"><span class="token string">'1010'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">:</span><span class="token format-spec">o</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>     <span class="token comment"># 输出八进制数值</span>
</span><span class="code-line"><span class="token string">'12'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">200</span><span class="token punctuation">:</span><span class="token format-spec">x</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>    <span class="token comment"># 输出十六进制数值</span>
</span><span class="code-line"><span class="token string">'c8'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">200</span><span class="token punctuation">:</span><span class="token format-spec">X</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'C8'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">345600000000</span><span class="token punctuation">:</span><span class="token format-spec">e</span><span class="token punctuation">}</span></span><span class="token string">'</span></span> <span class="token comment"># 科学计数法</span>
</span><span class="code-line"><span class="token string">'3.456000e+11'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">65</span><span class="token punctuation">:</span><span class="token format-spec">c</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>       <span class="token comment"># 将整数转化为一个字符后输出</span>
</span><span class="code-line"><span class="token string">'A'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">:</span><span class="token format-spec">#b</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>      <span class="token comment"># [类型] 带符号（基础）</span>
</span><span class="code-line"><span class="token string">'0b1010'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">:</span><span class="token format-spec">#o</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'0o12'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">:</span><span class="token format-spec">#x</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'0xa'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="显示正负号"><a aria-hidden="true" tabindex="-1" href="#显示正负号"><span class="icon icon-link"></span></a>显示正负号</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">12345</span><span class="token punctuation">:</span><span class="token format-spec">+</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>      <span class="token comment"># 显示正数的正号</span>
</span><span class="code-line"><span class="token string">'+12345'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token operator">-</span><span class="token number">12345</span><span class="token punctuation">:</span><span class="token format-spec">+</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>     <span class="token comment"># 显示负数的负号</span>
</span><span class="code-line"><span class="token string">'-12345'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token operator">-</span><span class="token number">12345</span><span class="token punctuation">:</span><span class="token format-spec">+10</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>   <span class="token comment"># 显示负号，并使用空格填充，直到长度为 10</span>
</span><span class="code-line"><span class="token string">'    -12345'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token operator">-</span><span class="token number">12345</span><span class="token punctuation">:</span><span class="token format-spec">+010</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>  <span class="token comment"># 显示负号，并使用0填充，直到长度为 10</span>
</span><span class="code-line"><span class="token string">'-000012345'</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="其它"><a aria-hidden="true" tabindex="-1" href="#其它"><span class="icon icon-link"></span></a>其它</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token operator">-</span><span class="token number">12345</span><span class="token punctuation">:</span><span class="token format-spec">0=10</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>  <span class="token comment"># 负数</span>
</span><span class="code-line"><span class="token string">'-000012345'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">12345</span><span class="token punctuation">:</span><span class="token format-spec">010</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>    <span class="token comment"># [0] 快捷方式（不对齐）</span>
</span><span class="code-line"><span class="token string">'0000012345'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token operator">-</span><span class="token number">12345</span><span class="token punctuation">:</span><span class="token format-spec">010</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'-000012345'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">import</span> math       <span class="token comment"># [.precision]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> math<span class="token punctuation">.</span>pi
</span><span class="code-line"><span class="token number">3.141592653589793</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span>math<span class="token punctuation">.</span>pi<span class="token punctuation">:</span><span class="token format-spec">.2f</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'3.14'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">1000000</span><span class="token punctuation">:</span><span class="token format-spec">,.2f</span><span class="token punctuation">}</span></span><span class="token string">'</span></span> <span class="token comment"># [分组选项]</span>
</span><span class="code-line"><span class="token string">'1,000,000.00'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">1000000</span><span class="token punctuation">:</span><span class="token format-spec">_.2f</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'1_000_000.00'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">0.25</span><span class="token punctuation">:</span><span class="token format-spec">0%</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>      <span class="token comment"># 百分比</span>
</span><span class="code-line"><span class="token string">'25.000000%'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token string-interpolation"><span class="token string">f'</span><span class="token interpolation"><span class="token punctuation">{</span><span class="token number">0.25</span><span class="token punctuation">:</span><span class="token format-spec">.0%</span><span class="token punctuation">}</span></span><span class="token string">'</span></span>
</span><span class="code-line"><span class="token string">'25%'</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-列表"><a aria-hidden="true" tabindex="-1" href="#python-列表"><span class="icon icon-link"></span></a>Python 列表</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义"><a aria-hidden="true" tabindex="-1" href="#定义"><span class="icon icon-link"></span></a>定义</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li1
</span><span class="code-line"><span class="token punctuation">[</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li2
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li3 <span class="token operator">=</span> <span class="token builtin">list</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li3
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li4 <span class="token operator">=</span> <span class="token builtin">list</span><span class="token punctuation">(</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li4
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="生成"><a aria-hidden="true" tabindex="-1" href="#生成"><span class="icon icon-link"></span></a>生成</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token builtin">list</span><span class="token punctuation">(</span><span class="token builtin">filter</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x <span class="token punctuation">:</span> x <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token punctuation">[</span>x <span class="token operator">**</span> <span class="token number">2</span> <span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token builtin">range</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">if</span>  x <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">49</span><span class="token punctuation">,</span> <span class="token number">81</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token punctuation">[</span>x <span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span> <span class="token keyword">if</span> x <span class="token operator">></span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token builtin">list</span><span class="token punctuation">(</span><span class="token builtin">filter</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x <span class="token operator">></span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="添加元素"><a aria-hidden="true" tabindex="-1" href="#添加元素"><span class="icon icon-link"></span></a>添加元素</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist col-span-2 row-span-3"><div class="wrap-header h3wrap"><h3 id="切片"><a aria-hidden="true" tabindex="-1" href="#切片"><span class="icon icon-link"></span></a>切片</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-3-->
<p>列表切片的语法：</p>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">a_list<span class="token punctuation">[</span>start<span class="token punctuation">:</span>end<span class="token punctuation">]</span>
</span><span class="code-line">a_list<span class="token punctuation">[</span>start<span class="token punctuation">:</span>end<span class="token punctuation">:</span>step<span class="token punctuation">]</span>
</span></code></pre>
<h4 id="切片-1"><a aria-hidden="true" tabindex="-1" href="#切片-1"><span class="icon icon-link"></span></a>切片</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'spam'</span><span class="token punctuation">,</span> <span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">,</span> <span class="token string">'lobster'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">4</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">]</span>
</span></code></pre>
<h4 id="省略索引"><a aria-hidden="true" tabindex="-1" href="#省略索引"><span class="icon icon-link"></span></a>省略索引</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">4</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'spam'</span><span class="token punctuation">,</span> <span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">4</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'spam'</span><span class="token punctuation">,</span> <span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">,</span> <span class="token string">'lobster'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">:</span><span class="token builtin">len</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">,</span> <span class="token string">'lobster'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'spam'</span><span class="token punctuation">,</span> <span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">,</span> <span class="token string">'lobster'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'spam'</span><span class="token punctuation">,</span> <span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">,</span> <span class="token string">'lobster'</span><span class="token punctuation">]</span>
</span></code></pre>
<h4 id="间隔索引"><a aria-hidden="true" tabindex="-1" href="#间隔索引"><span class="icon icon-link"></span></a>间隔索引</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token string">'spam'</span><span class="token punctuation">,</span> <span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">,</span> <span class="token string">'lobster'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">6</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'spam'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">6</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'lobster'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">:</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'lobster'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'egg'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'spam'</span><span class="token punctuation">,</span> <span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">,</span> <span class="token string">'lobster'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'lobster'</span><span class="token punctuation">,</span> <span class="token string">'ham'</span><span class="token punctuation">,</span> <span class="token string">'tomato'</span><span class="token punctuation">,</span> <span class="token string">'bacon'</span><span class="token punctuation">,</span> <span class="token string">'egg'</span><span class="token punctuation">,</span> <span class="token string">'spam'</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除"><a aria-hidden="true" tabindex="-1" href="#删除"><span class="icon icon-link"></span></a>删除</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'bread'</span><span class="token punctuation">,</span> <span class="token string">'butter'</span><span class="token punctuation">,</span> <span class="token string">'milk'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token string">'milk'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'bread'</span><span class="token punctuation">,</span> <span class="token string">'butter'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> <span class="token keyword">del</span> li<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'butter'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">.</span>remove<span class="token punctuation">(</span><span class="token string">'butter'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="列表边界"><a aria-hidden="true" tabindex="-1" href="#列表边界"><span class="icon icon-link"></span></a>列表边界</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'a'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token string">'d'</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span>
</span><span class="code-line">Traceback <span class="token punctuation">(</span>most recent call last<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">  File <span class="token string">"&#x3C;stdin>"</span><span class="token punctuation">,</span> line <span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">in</span> <span class="token operator">&#x3C;</span>module<span class="token operator">></span>
</span><span class="code-line">IndexError<span class="token punctuation">:</span> <span class="token builtin">list</span> index out of <span class="token builtin">range</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="连接"><a aria-hidden="true" tabindex="-1" href="#连接"><span class="icon icon-link"></span></a>连接</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> odd <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> odd<span class="token punctuation">.</span>extend<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> odd
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> odd <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> odd <span class="token operator">+</span> <span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="排序和反转"><a aria-hidden="true" tabindex="-1" href="#排序和反转"><span class="icon icon-link"></span></a>排序和反转</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">.</span>sort<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">.</span>reverse<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="计数"><a aria-hidden="true" tabindex="-1" href="#计数"><span class="icon icon-link"></span></a>计数</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li<span class="token punctuation">.</span>count<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token number">2</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="重复"><a aria-hidden="true" tabindex="-1" href="#重复"><span class="icon icon-link"></span></a>重复</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"re"</span><span class="token punctuation">]</span> <span class="token operator">*</span> <span class="token number">3</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> li
</span><span class="code-line"><span class="token punctuation">[</span><span class="token string">'re'</span><span class="token punctuation">,</span> <span class="token string">'re'</span><span class="token punctuation">,</span> <span class="token string">'re'</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="搜索"><a aria-hidden="true" tabindex="-1" href="#搜索"><span class="icon icon-link"></span></a>搜索</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> nums <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">20.5</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">999</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> nums<span class="token punctuation">.</span>index<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token number">3</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> nums<span class="token punctuation">.</span>index<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span>  <span class="token comment"># 搜索3-7之间的元素</span>
</span><span class="code-line"><span class="token number">5</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> nums<span class="token punctuation">.</span>index<span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment"># 搜索4之后的元素</span>
</span><span class="code-line"><span class="token number">6</span>
</span></code></pre>
<p>当寻找一个不存在的值时，抛出<code>ValueError</code>。</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-判断"><a aria-hidden="true" tabindex="-1" href="#python-判断"><span class="icon icon-link"></span></a>Python 判断</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="if-else"><a aria-hidden="true" tabindex="-1" href="#if-else"><span class="icon icon-link"></span></a>if-else</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">number <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">'输入一个整数：'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">if</span> number <span class="token operator">&#x3C;</span> <span class="token number">0</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"您输入了一个负数。"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">else</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"您输入了一个非负整数。"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="if-elif-else"><a aria-hidden="true" tabindex="-1" href="#if-elif-else"><span class="icon icon-link"></span></a>if-elif-else</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">number <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">'输入一个整数：'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">if</span> number <span class="token operator">&#x3C;</span> <span class="token number">0</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"您输入了一个负数。"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">elif</span> number <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"您输入了一个 0 。"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">else</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"您输入了一个正数。"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="三目运算"><a aria-hidden="true" tabindex="-1" href="#三目运算"><span class="icon icon-link"></span></a>三目运算</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">scope <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">'输入百分制成绩：'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">line <span class="token operator">=</span> <span class="token number">60</span>
</span><span class="code-line">tip <span class="token operator">=</span> <span class="token string">"及格"</span> <span class="token keyword">if</span> scope <span class="token operator">>=</span> line <span class="token keyword">else</span> <span class="token string">"不及格"</span>
</span><span class="code-line"><span class="token comment"># 相当于 scope >= line ? "及格" : "不及格"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>tip<span class="token punctuation">)</span>
</span></code></pre>
<p>注意条件是放在中间的</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-循环"><a aria-hidden="true" tabindex="-1" href="#python-循环"><span class="icon icon-link"></span></a>Python 循环</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="一般形式"><a aria-hidden="true" tabindex="-1" href="#一般形式"><span class="icon icon-link"></span></a>一般形式</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">for</span> prime <span class="token keyword">in</span> primes<span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>prime<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="带索引"><a aria-hidden="true" tabindex="-1" href="#带索引"><span class="icon icon-link"></span></a>带索引</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">animals <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"dog"</span><span class="token punctuation">,</span> <span class="token string">"cat"</span><span class="token punctuation">,</span> <span class="token string">"mouse"</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">for</span> i<span class="token punctuation">,</span> value <span class="token keyword">in</span> <span class="token builtin">enumerate</span><span class="token punctuation">(</span>animals<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> value<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="while-循环"><a aria-hidden="true" tabindex="-1" href="#while-循环"><span class="icon icon-link"></span></a>while 循环</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">x <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line"><span class="token keyword">while</span> x <span class="token operator">&#x3C;</span> <span class="token number">4</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
</span><span class="code-line">    x <span class="token operator">+=</span> <span class="token number">1</span>  <span class="token comment"># Shorthand for x = x + 1</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="跳出循环"><a aria-hidden="true" tabindex="-1" href="#跳出循环"><span class="icon icon-link"></span></a>跳出循环</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">x <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line"><span class="token keyword">for</span> index <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    x <span class="token operator">=</span> index <span class="token operator">*</span> <span class="token number">10</span>
</span><span class="code-line">    <span class="token keyword">if</span> index <span class="token operator">==</span> <span class="token number">5</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">break</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="跳过一轮循环"><a aria-hidden="true" tabindex="-1" href="#跳过一轮循环"><span class="icon icon-link"></span></a>跳过一轮循环</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">for</span> index <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">:</span> 
</span><span class="code-line">    x <span class="token operator">=</span> index <span class="token operator">*</span> <span class="token number">10</span>
</span><span class="code-line">    <span class="token keyword">if</span> index <span class="token operator">==</span> <span class="token number">5</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">continue</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="范围循环"><a aria-hidden="true" tabindex="-1" href="#范围循环"><span class="icon icon-link"></span></a>范围循环</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token comment"># Prints: 0 1 2 3</span>
</span><span class="code-line"><span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token comment"># Prints: 4 5 6 7</span>
</span><span class="code-line"><span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token comment"># Prints: 4 6 8</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用-zip"><a aria-hidden="true" tabindex="-1" href="#使用-zip"><span class="icon icon-link"></span></a>使用 zip()</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">name <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'Pete'</span><span class="token punctuation">,</span> <span class="token string">'John'</span><span class="token punctuation">,</span> <span class="token string">'Elizabeth'</span><span class="token punctuation">]</span>
</span><span class="code-line">age <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">44</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">for</span> n<span class="token punctuation">,</span> a <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> age<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'%s is %d years old'</span> <span class="token operator">%</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> a<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="列表生成式"><a aria-hidden="true" tabindex="-1" href="#列表生成式"><span class="icon icon-link"></span></a>列表生成式</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">result <span class="token operator">=</span> <span class="token punctuation">[</span>x<span class="token operator">**</span><span class="token number">2</span> <span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token keyword">if</span> x <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">]</span>
</span><span class="code-line"> 
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># [0, 4, 16, 36, 64]</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-函数"><a aria-hidden="true" tabindex="-1" href="#python-函数"><span class="icon icon-link"></span></a>Python 函数</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="基础"><a aria-hidden="true" tabindex="-1" href="#基础"><span class="icon icon-link"></span></a>基础</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">hello_world</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>  
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Hello, World!'</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="返回"><a aria-hidden="true" tabindex="-1" href="#返回"><span class="icon icon-link"></span></a>返回</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"x is %s, y is %s"</span> <span class="token operator">%</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line highlight-line">    <span class="token keyword">return</span> x <span class="token operator">+</span> y
</span><span class="code-line">add<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span>    <span class="token comment"># => 11</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="位置参数"><a aria-hidden="true" tabindex="-1" href="#位置参数"><span class="icon icon-link"></span></a>位置参数</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">varargs</span><span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> args
</span><span class="code-line">varargs<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>  <span class="token comment"># => (1, 2, 3)</span>
</span></code></pre>
<p>args 的类型是 tuple</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="关键字参数"><a aria-hidden="true" tabindex="-1" href="#关键字参数"><span class="icon icon-link"></span></a>关键字参数</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">keyword_args</span><span class="token punctuation">(</span><span class="token operator">**</span>kwargs<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> kwargs
</span><span class="code-line"><span class="token comment"># => {"big": "foot", "loch": "ness"}</span>
</span><span class="code-line">keyword_args<span class="token punctuation">(</span>big<span class="token operator">=</span><span class="token string">"foot"</span><span class="token punctuation">,</span> loch<span class="token operator">=</span><span class="token string">"ness"</span><span class="token punctuation">)</span>
</span></code></pre>
<p>kwargs 的类型是 dict</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="返回多个"><a aria-hidden="true" tabindex="-1" href="#返回多个"><span class="icon icon-link"></span></a>返回多个</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">swap</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> y<span class="token punctuation">,</span> x
</span><span class="code-line">x <span class="token operator">=</span> <span class="token number">1</span>
</span><span class="code-line">y <span class="token operator">=</span> <span class="token number">2</span>
</span><span class="code-line">x<span class="token punctuation">,</span> y <span class="token operator">=</span> swap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span>  <span class="token comment"># => x = 2, y = 1</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="默认值"><a aria-hidden="true" tabindex="-1" href="#默认值"><span class="icon icon-link"></span></a>默认值</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> x <span class="token operator">+</span> y
</span><span class="code-line">add<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>      <span class="token comment"># => 15</span>
</span><span class="code-line">add<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>  <span class="token comment"># => 25</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="匿名函数"><a aria-hidden="true" tabindex="-1" href="#匿名函数"><span class="icon icon-link"></span></a>匿名函数</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token comment"># => True</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x <span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># => 5</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">,</span> y<span class="token punctuation">:</span> x <span class="token operator">**</span> <span class="token number">2</span> <span class="token operator">+</span> y <span class="token operator">**</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-解包"><a aria-hidden="true" tabindex="-1" href="#python-解包"><span class="icon icon-link"></span></a>Python 解包</h2><div class="wrap-body">
<ul>
<li>解包是将一个
<a href="https://docs.python.org/zh-cn/3/library/stdtypes.html#sequence-types-list-tuple-range">序列</a>
内的多个元素依次重新分配到有限个容器的过程，这只发生在 <strong>变量赋值</strong>、<strong>参数传递</strong> 和 <strong>生成式生成</strong> 过程中。</li>
<li><code>_</code> 这个变量是命令行交互中最后一次计算得到的值，在程序设计中一般用来存放解包时不再需要的值。
但它的含义会因赋值而改变，比如标准库 <a href="https://docs.python.org/zh-cn/3/library/gettext.html">gettext</a> 中常用作动态获取翻译文本。</li>
</ul>
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="等量解包"><a aria-hidden="true" tabindex="-1" href="#等量解包"><span class="icon icon-link"></span></a>等量解包</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">ip<span class="token punctuation">,</span> port <span class="token operator">=</span> <span class="token string">"127.0.0.1"</span><span class="token punctuation">,</span> <span class="token number">80</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>ip<span class="token punctuation">)</span>  <span class="token comment"># -> "127.0.0.1"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>port<span class="token punctuation">)</span>  <span class="token comment"># -> 80</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 与以下代码等价</span>
</span><span class="code-line">ip<span class="token punctuation">,</span> port <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">"127.0.0.1"</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 与以下代码效果相同</span>
</span><span class="code-line">ip<span class="token punctuation">,</span> port <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"127.0.0.1"</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="适量解包"><a aria-hidden="true" tabindex="-1" href="#适量解包"><span class="icon icon-link"></span></a>适量解包</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">ip<span class="token punctuation">,</span> _<span class="token punctuation">,</span> port <span class="token operator">=</span> <span class="token string">"127.0.0.1:80"</span><span class="token punctuation">.</span>rpartition<span class="token punctuation">(</span><span class="token string">":"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>ip<span class="token punctuation">)</span>  <span class="token comment"># -> "127.0.0.1"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>port<span class="token punctuation">)</span>  <span class="token comment"># -> "80"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># _ 这个变量此刻的值是 ":" ，但一般不再使用。</span>
</span></code></pre>
<p><code>_</code> 也是一个单一变量，不允许解包多个元素，因此变量与值必须一一对应。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="过量解包"><a aria-hidden="true" tabindex="-1" href="#过量解包"><span class="icon icon-link"></span></a>过量解包</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">major<span class="token punctuation">,</span> minor<span class="token punctuation">,</span> <span class="token operator">*</span>parts <span class="token operator">=</span> <span class="token string">"3.10.2.beta"</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>major<span class="token punctuation">)</span>  <span class="token comment"># -> "3"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>minor<span class="token punctuation">)</span>  <span class="token comment"># -> "10"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>parts<span class="token punctuation">)</span>  <span class="token comment"># -> ["2", "beta"]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 可将 parts 改为 _ 来表示不再需要后面的元素</span>
</span></code></pre>
<p>这里的 <code>*</code> 就是收集<a href="https://docs.python.org/zh-cn/3/library/stdtypes.html#sequence-types-list-tuple-range">序列</a>在解包过程中多出来的元素，
只能有一个，与向函数传递<a href="#%E4%BD%8D%E7%BD%AE%E5%8F%82%E6%95%B0">位置参数</a>时的 <code>*</code> 别无二致。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="解包取左边"><a aria-hidden="true" tabindex="-1" href="#解包取左边"><span class="icon icon-link"></span></a>解包取左边</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">major<span class="token punctuation">,</span> minor<span class="token punctuation">,</span> <span class="token operator">*</span>_ <span class="token operator">=</span> <span class="token string">"3.10.2.beta"</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>major<span class="token punctuation">)</span>  <span class="token comment"># -> "3"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>minor<span class="token punctuation">)</span>  <span class="token comment"># -> "10"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="解包取两边"><a aria-hidden="true" tabindex="-1" href="#解包取两边"><span class="icon icon-link"></span></a>解包取两边</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">major<span class="token punctuation">,</span> <span class="token operator">*</span>_<span class="token punctuation">,</span> level <span class="token operator">=</span> <span class="token string">"3.10.2.beta"</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>major<span class="token punctuation">)</span>  <span class="token comment"># -> "3"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>level<span class="token punctuation">)</span>  <span class="token comment"># -> "beta"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="解包取右边"><a aria-hidden="true" tabindex="-1" href="#解包取右边"><span class="icon icon-link"></span></a>解包取右边</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">*</span>_<span class="token punctuation">,</span> micro<span class="token punctuation">,</span> level <span class="token operator">=</span> <span class="token string">"3.10.2.beta"</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>micro<span class="token punctuation">)</span>  <span class="token comment"># -> "2"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>level<span class="token punctuation">)</span>  <span class="token comment"># -> "beta"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="解包集合"><a aria-hidden="true" tabindex="-1" href="#解包集合"><span class="icon icon-link"></span></a>解包集合</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> <span class="token operator">*</span>_ <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>  <span class="token comment"># -> 1</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>  <span class="token comment"># -> 2</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>_<span class="token punctuation">)</span>  <span class="token comment"># -> [3]</span>
</span></code></pre>
<p><a href="https://docs.python.org/zh-cn/3/library/stdtypes.html#set-types-set-frozenset">集合</a>
中的元素是无序的，因此解包结果不能轻易确定。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="解包迭代器"><a aria-hidden="true" tabindex="-1" href="#解包迭代器"><span class="icon icon-link"></span></a>解包迭代器</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> <span class="token operator">*</span>_ <span class="token operator">=</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>  <span class="token comment"># -> 0</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>  <span class="token comment"># -> 1</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>_<span class="token punctuation">)</span>  <span class="token comment"># -> [2]</span>
</span></code></pre>
<p>支持
<a href="https://docs.python.org/zh-cn/3/library/stdtypes.html#iterator-types">迭代器</a>
协议的对象也可被解包。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="解包字典"><a aria-hidden="true" tabindex="-1" href="#解包字典"><span class="icon icon-link"></span></a>解包字典</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> <span class="token operator">*</span>_ <span class="token operator">=</span> <span class="token builtin">dict</span><span class="token punctuation">(</span>a<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> b<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>  <span class="token comment"># -> "a"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>_<span class="token punctuation">)</span>  <span class="token comment"># -> ["c"]</span>
</span><span class="code-line">a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> <span class="token operator">*</span>_ <span class="token operator">=</span> <span class="token builtin">dict</span><span class="token punctuation">(</span>a<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> b<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span>values<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>  <span class="token comment"># -> 1</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>_<span class="token punctuation">)</span>  <span class="token comment"># -> [3]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="生成式中的解包"><a aria-hidden="true" tabindex="-1" href="#生成式中的解包"><span class="icon icon-link"></span></a>生成式中的解包</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">chars <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">*</span><span class="token string">"abc"</span><span class="token punctuation">,</span> <span class="token operator">*</span><span class="token string">"def"</span><span class="token punctuation">,</span> <span class="token string">"g"</span><span class="token punctuation">,</span> <span class="token string">"h"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># -> ("a", "b", "c", "d", "e", "f", "g", "h")</span>
</span><span class="code-line">
</span><span class="code-line">digits <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token operator">*</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">*</span><span class="token string">"abcdef"</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,</span>
</span><span class="code-line"><span class="token comment">#     "a", "b", "c", "d", "e", "f"]</span>
</span><span class="code-line">
</span><span class="code-line">part <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"小明"</span><span class="token punctuation">:</span> <span class="token number">18</span><span class="token punctuation">,</span> <span class="token string">"小亮"</span><span class="token punctuation">:</span> <span class="token number">22</span><span class="token punctuation">}</span>
</span><span class="code-line">summary <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"小花"</span><span class="token punctuation">:</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token operator">**</span>part<span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>summary<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># -> {"小花": 16, "小明": 18, "小亮": 22}</span>
</span></code></pre>
<ul>
<li>仅在列表／元组生成式中可以使用多个 <code>*</code></li>
<li>仅在字典生成式中可以使用多个 <code>**</code></li>
</ul>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="迭代中解包"><a aria-hidden="true" tabindex="-1" href="#迭代中解包"><span class="icon icon-link"></span></a>迭代中解包</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">students <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">    <span class="token punctuation">(</span><span class="token string">"小明"</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">    <span class="token punctuation">(</span><span class="token string">"小亮"</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">for</span> k<span class="token punctuation">,</span> v <span class="token keyword">in</span> students<span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span>  <span class="token comment"># -> "小明"、"小亮"</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span>  <span class="token comment"># -> 18、22</span>
</span><span class="code-line">
</span><span class="code-line">students <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">    <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">"小明"</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">    <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">"小亮"</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">for</span> i<span class="token punctuation">,</span> <span class="token punctuation">(</span>k<span class="token punctuation">,</span> v<span class="token punctuation">)</span> <span class="token keyword">in</span> students<span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>  <span class="token comment"># -> 0、1</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>k<span class="token punctuation">)</span>  <span class="token comment"># -> "小明"、"小亮"</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span>  <span class="token comment"># -> 18、22</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="函数中的解包"><a aria-hidden="true" tabindex="-1" href="#函数中的解包"><span class="icon icon-link"></span></a>函数中的解包</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">version</span><span class="token punctuation">(</span>major<span class="token punctuation">,</span> minor<span class="token punctuation">,</span> <span class="token operator">*</span>parts<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>major<span class="token punctuation">)</span>  <span class="token comment"># -> "3"</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>minor<span class="token punctuation">)</span>  <span class="token comment"># -> "10"</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span>parts<span class="token punctuation">)</span>  <span class="token comment"># -> ("2", "beta", "0")</span>
</span><span class="code-line">
</span><span class="code-line">version<span class="token punctuation">(</span><span class="token string">"3"</span><span class="token punctuation">,</span> <span class="token string">"10"</span><span class="token punctuation">,</span> <span class="token string">"2"</span><span class="token punctuation">,</span> <span class="token string">"beta"</span><span class="token punctuation">,</span> <span class="token string">"0"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 过程类似于</span>
</span><span class="code-line">major<span class="token punctuation">,</span> minor<span class="token punctuation">,</span> <span class="token operator">*</span>parts <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">"3"</span><span class="token punctuation">,</span> <span class="token string">"10"</span><span class="token punctuation">,</span> <span class="token string">"2"</span><span class="token punctuation">,</span> <span class="token string">"beta"</span><span class="token punctuation">,</span> <span class="token string">"0"</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">version</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    parts <span class="token operator">=</span> <span class="token string">"3.10.2.beta.0"</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token operator">*</span>parts<span class="token punctuation">,</span> <span class="token string">"x64"</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>version<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># -> ("3", "10", "2", "beta", "0", "x64")</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-模块"><a aria-hidden="true" tabindex="-1" href="#python-模块"><span class="icon icon-link"></span></a>Python 模块</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="导入模块"><a aria-hidden="true" tabindex="-1" href="#导入模块"><span class="icon icon-link"></span></a>导入模块</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">import</span> math
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>math<span class="token punctuation">.</span>sqrt<span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># => 4.0</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="从一个模块导入"><a aria-hidden="true" tabindex="-1" href="#从一个模块导入"><span class="icon icon-link"></span></a>从一个模块导入</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">from</span> math <span class="token keyword">import</span> ceil<span class="token punctuation">,</span> floor
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>ceil<span class="token punctuation">(</span><span class="token number">3.7</span><span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment"># => 4.0</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>floor<span class="token punctuation">(</span><span class="token number">3.7</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># => 3.0</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="导入一个模块的全部"><a aria-hidden="true" tabindex="-1" href="#导入一个模块的全部"><span class="icon icon-link"></span></a>导入一个模块的全部</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">from</span> math <span class="token keyword">import</span> <span class="token operator">*</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="给模块起别名"><a aria-hidden="true" tabindex="-1" href="#给模块起别名"><span class="icon icon-link"></span></a>给模块起别名</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">import</span> math <span class="token keyword">as</span> m
</span><span class="code-line"><span class="token comment"># => True</span>
</span><span class="code-line">math<span class="token punctuation">.</span>sqrt<span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span> <span class="token operator">==</span> m<span class="token punctuation">.</span>sqrt<span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="浏览模块的函数和属性"><a aria-hidden="true" tabindex="-1" href="#浏览模块的函数和属性"><span class="icon icon-link"></span></a>浏览模块的函数和属性</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">import</span> math
</span><span class="code-line"><span class="token builtin">dir</span><span class="token punctuation">(</span>math<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-文件处理"><a aria-hidden="true" tabindex="-1" href="#python-文件处理"><span class="icon icon-link"></span></a>Python 文件处理</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="读取文件"><a aria-hidden="true" tabindex="-1" href="#读取文件"><span class="icon icon-link"></span></a>读取文件</h3><div class="wrap-body">
<h4 id="逐行"><a aria-hidden="true" tabindex="-1" href="#逐行"><span class="icon icon-link"></span></a>逐行</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"myfile.txt"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token builtin">file</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">for</span> line <span class="token keyword">in</span> <span class="token builtin">file</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span>
</span></code></pre>
<h4 id="带行号"><a aria-hidden="true" tabindex="-1" href="#带行号"><span class="icon icon-link"></span></a>带行号</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token builtin">file</span> <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">,</span> <span class="token string">'r'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">for</span> i<span class="token punctuation">,</span> line <span class="token keyword">in</span> <span class="token builtin">enumerate</span><span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">,</span> start<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Number %s: %s"</span> <span class="token operator">%</span> <span class="token punctuation">(</span>i<span class="token punctuation">,</span> line<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="字符串-1"><a aria-hidden="true" tabindex="-1" href="#字符串-1"><span class="icon icon-link"></span></a>字符串</h3><div class="wrap-body">
<h4 id="写入一个字符串"><a aria-hidden="true" tabindex="-1" href="#写入一个字符串"><span class="icon icon-link"></span></a>写入一个字符串</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">contents <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"aa"</span><span class="token punctuation">:</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token string">"bb"</span><span class="token punctuation">:</span> <span class="token number">21</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"myfile1.txt"</span><span class="token punctuation">,</span> <span class="token string">"w+"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token builtin">file</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token builtin">file</span><span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">(</span>contents<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
<h4 id="读取一个字符串"><a aria-hidden="true" tabindex="-1" href="#读取一个字符串"><span class="icon icon-link"></span></a>读取一个字符串</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'myfile1.txt'</span><span class="token punctuation">,</span> <span class="token string">"r+"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token builtin">file</span><span class="token punctuation">:</span>
</span><span class="code-line">    contents <span class="token operator">=</span> <span class="token builtin">file</span><span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>contents<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="对象"><a aria-hidden="true" tabindex="-1" href="#对象"><span class="icon icon-link"></span></a>对象</h3><div class="wrap-body">
<h4 id="写一个对象"><a aria-hidden="true" tabindex="-1" href="#写一个对象"><span class="icon icon-link"></span></a>写一个对象</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">contents <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"aa"</span><span class="token punctuation">:</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token string">"bb"</span><span class="token punctuation">:</span> <span class="token number">21</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"myfile2.txt"</span><span class="token punctuation">,</span> <span class="token string">"w+"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token builtin">file</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token builtin">file</span><span class="token punctuation">.</span>write<span class="token punctuation">(</span>json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>contents<span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
<h4 id="读取对象"><a aria-hidden="true" tabindex="-1" href="#读取对象"><span class="icon icon-link"></span></a>读取对象</h4>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'myfile2.txt'</span><span class="token punctuation">,</span> <span class="token string">"r+"</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token builtin">file</span><span class="token punctuation">:</span>
</span><span class="code-line">    contents <span class="token operator">=</span> json<span class="token punctuation">.</span>load<span class="token punctuation">(</span><span class="token builtin">file</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>contents<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除文件"><a aria-hidden="true" tabindex="-1" href="#删除文件"><span class="icon icon-link"></span></a>删除文件</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">import</span> os
</span><span class="code-line">os<span class="token punctuation">.</span>remove<span class="token punctuation">(</span><span class="token string">"myfile.txt"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查和删除"><a aria-hidden="true" tabindex="-1" href="#检查和删除"><span class="icon icon-link"></span></a>检查和删除</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">import</span> os
</span><span class="code-line"><span class="token keyword">if</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span><span class="token string">"myfile.txt"</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    os<span class="token punctuation">.</span>remove<span class="token punctuation">(</span><span class="token string">"myfile.txt"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">else</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The file does not exist"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除文件夹"><a aria-hidden="true" tabindex="-1" href="#删除文件夹"><span class="icon icon-link"></span></a>删除文件夹</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">import</span> os
</span><span class="code-line">os<span class="token punctuation">.</span>rmdir<span class="token punctuation">(</span><span class="token string">"myfolder"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-类和继承"><a aria-hidden="true" tabindex="-1" href="#python-类和继承"><span class="icon icon-link"></span></a>Python 类和继承</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义-1"><a aria-hidden="true" tabindex="-1" href="#定义-1"><span class="icon icon-link"></span></a>定义</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">MyNewClass</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">pass</span>
</span><span class="code-line"><span class="token comment"># 类的实例化</span>
</span><span class="code-line">my <span class="token operator">=</span> MyNewClass<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="构造函数"><a aria-hidden="true" tabindex="-1" href="#构造函数"><span class="icon icon-link"></span></a>构造函数</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Animal</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> voice<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        self<span class="token punctuation">.</span>voice <span class="token operator">=</span> voice
</span><span class="code-line"> 
</span><span class="code-line">cat <span class="token operator">=</span> Animal<span class="token punctuation">(</span><span class="token string">'Meow'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>cat<span class="token punctuation">.</span>voice<span class="token punctuation">)</span>    <span class="token comment"># => Meow</span>
</span><span class="code-line"> 
</span><span class="code-line">dog <span class="token operator">=</span> Animal<span class="token punctuation">(</span><span class="token string">'Woof'</span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>dog<span class="token punctuation">.</span>voice<span class="token punctuation">)</span>    <span class="token comment"># => Woof</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="方法"><a aria-hidden="true" tabindex="-1" href="#方法"><span class="icon icon-link"></span></a>方法</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Dog</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token comment"># 类的方法</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">bark</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Ham-Ham"</span><span class="token punctuation">)</span>
</span><span class="code-line"> 
</span><span class="code-line">charlie <span class="token operator">=</span> Dog<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">charlie<span class="token punctuation">.</span>bark<span class="token punctuation">(</span><span class="token punctuation">)</span>   <span class="token comment"># => "Ham-Ham"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="类变量"><a aria-hidden="true" tabindex="-1" href="#类变量"><span class="icon icon-link"></span></a>类变量</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">MyClass</span><span class="token punctuation">:</span>
</span><span class="code-line">    class_variable <span class="token operator">=</span> <span class="token string">"A class variable!"</span>
</span><span class="code-line"><span class="token comment"># => 一个类变量！</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>MyClass<span class="token punctuation">.</span>class_variable<span class="token punctuation">)</span>
</span><span class="code-line">x <span class="token operator">=</span> MyClass<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"> 
</span><span class="code-line"><span class="token comment"># => 一个类变量！</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">.</span>class_variable<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="super-函数"><a aria-hidden="true" tabindex="-1" href="#super-函数"><span class="icon icon-link"></span></a>super() 函数</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">ParentClass</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">print_test</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Parent Method"</span><span class="token punctuation">)</span>
</span><span class="code-line"> 
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">ChildClass</span><span class="token punctuation">(</span>ParentClass<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">print_test</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Child Method"</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token comment"># 调用父级的 print_test()</span>
</span><span class="code-line">        <span class="token builtin">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>print_test<span class="token punctuation">(</span><span class="token punctuation">)</span> 
</span></code></pre>
<hr>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> child_instance <span class="token operator">=</span> ChildClass<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> child_instance<span class="token punctuation">.</span>print_test<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">Child Method
</span><span class="code-line">Parent Method
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="repr-方法"><a aria-hidden="true" tabindex="-1" href="#repr-方法"><span class="icon icon-link"></span></a>repr() 方法</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Employee</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> name<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        self<span class="token punctuation">.</span>name <span class="token operator">=</span> name
</span><span class="code-line"> 
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__repr__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">return</span> self<span class="token punctuation">.</span>name
</span><span class="code-line"> 
</span><span class="code-line">john <span class="token operator">=</span> Employee<span class="token punctuation">(</span><span class="token string">'John'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>john<span class="token punctuation">)</span>  <span class="token comment"># => John</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="用户定义的异常"><a aria-hidden="true" tabindex="-1" href="#用户定义的异常"><span class="icon icon-link"></span></a>用户定义的异常</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">CustomError</span><span class="token punctuation">(</span>Exception<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">pass</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多态性"><a aria-hidden="true" tabindex="-1" href="#多态性"><span class="icon icon-link"></span></a>多态性</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">ParentClass</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">print_self</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span>
</span><span class="code-line"> 
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">ChildClass</span><span class="token punctuation">(</span>ParentClass<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">print_self</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'B'</span><span class="token punctuation">)</span>
</span><span class="code-line"> 
</span><span class="code-line">obj_A <span class="token operator">=</span> ParentClass<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">obj_B <span class="token operator">=</span> ChildClass<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"> 
</span><span class="code-line">obj_A<span class="token punctuation">.</span>print_self<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># => A</span>
</span><span class="code-line">obj_B<span class="token punctuation">.</span>print_self<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># => B</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="重写"><a aria-hidden="true" tabindex="-1" href="#重写"><span class="icon icon-link"></span></a>重写</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">ParentClass</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">print_self</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Parent"</span><span class="token punctuation">)</span>
</span><span class="code-line"> 
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">ChildClass</span><span class="token punctuation">(</span>ParentClass<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">print_self</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Child"</span><span class="token punctuation">)</span>
</span><span class="code-line"> 
</span><span class="code-line">child_instance <span class="token operator">=</span> ChildClass<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">child_instance<span class="token punctuation">.</span>print_self<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># => Child</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="继承"><a aria-hidden="true" tabindex="-1" href="#继承"><span class="icon icon-link"></span></a>继承</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Animal</span><span class="token punctuation">:</span> 
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> name<span class="token punctuation">,</span> legs<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        self<span class="token punctuation">.</span>name <span class="token operator">=</span> name
</span><span class="code-line">        self<span class="token punctuation">.</span>legs <span class="token operator">=</span> legs
</span><span class="code-line">        
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span>Animal<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">sound</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Woof!"</span><span class="token punctuation">)</span>
</span><span class="code-line"> 
</span><span class="code-line">Yoki <span class="token operator">=</span> Dog<span class="token punctuation">(</span><span class="token string">"Yoki"</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>Yoki<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token comment"># => YOKI</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>Yoki<span class="token punctuation">.</span>legs<span class="token punctuation">)</span> <span class="token comment"># => 4</span>
</span><span class="code-line">Yoki<span class="token punctuation">.</span>sound<span class="token punctuation">(</span><span class="token punctuation">)</span>     <span class="token comment"># => Woof!</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-数据模型"><a aria-hidden="true" tabindex="-1" href="#python-数据模型"><span class="icon icon-link"></span></a>Python 数据模型</h2><div class="wrap-body">
<p>更多请移步 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html">https://docs.python.org/zh-cn/3/reference/datamodel.html</a></p>
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="自定义类创建"><a aria-hidden="true" tabindex="-1" href="#自定义类创建"><span class="icon icon-link"></span></a>自定义类创建</h3><div class="wrap-body">
<p>参见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#customizing-class-creation">自定义类创建</a> 。</p>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">from</span> typing <span class="token keyword">import</span> Any
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Object</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__new__</span><span class="token punctuation">(</span>cls<span class="token punctuation">,</span> <span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token string">"self"</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token comment"># new 和 init 声明的参数必须一致</span>
</span><span class="code-line">        <span class="token comment"># 或者用 *args 和 **kwargs 进行兼容</span>
</span><span class="code-line">        <span class="token keyword">return</span> <span class="token builtin">object</span><span class="token punctuation">.</span>__new__<span class="token punctuation">(</span>cls<span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> <span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token comment"># 初始化方法没有返回值，也不能返回值。</span>
</span><span class="code-line">        <span class="token keyword">pass</span>
</span><span class="code-line">
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__call__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> <span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Any<span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">pass</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 依次调用了 new 和 init，所以如果</span>
</span><span class="code-line"><span class="token comment"># 手动调用 new，那么别忘了调用 init</span>
</span><span class="code-line">obj <span class="token operator">=</span> Object<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 触发 __call__ 方法，要给什么参数取决于声明</span>
</span><span class="code-line">obj<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="上下文管理器"><a aria-hidden="true" tabindex="-1" href="#上下文管理器"><span class="icon icon-link"></span></a>上下文管理器</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<p>参见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#with-statement-context-managers">上下文管理器</a> 。</p>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">from</span> typing <span class="token keyword">import</span> Any
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Object</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__enter__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Optional<span class="token punctuation">[</span>Any<span class="token punctuation">]</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token comment"># with 语句会将返回值绑定到 as 子句中的变量，如果有的话。</span>
</span><span class="code-line">        <span class="token keyword">return</span>
</span><span class="code-line">
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__exit__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> exc_type<span class="token punctuation">,</span> exc_value<span class="token punctuation">,</span> traceback<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token comment"># 若 with 内没有发生异常，则三个参数都是 None 。</span>
</span><span class="code-line">        <span class="token comment"># 不应该重新引发传入的异常，这是调用者的责任。</span>
</span><span class="code-line">        <span class="token keyword">pass</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">with</span> Object<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> alias<span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token comment"># 进入 with 之前调用 obj.__enter__() 并得到 alias（如果有返回的话）</span>
</span><span class="code-line">    <span class="token keyword">pass</span>
</span><span class="code-line"><span class="token comment"># 离开 with 后调用 obj.__exit__() ，不管是正常结束还是因异常抛出而离开。</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 当需要获取 Object 的对象时可以这样写</span>
</span><span class="code-line">obj <span class="token operator">=</span> Object<span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">with</span> obj <span class="token keyword">as</span> alias<span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">pass</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="特殊方法"><a aria-hidden="true" tabindex="-1" href="#特殊方法"><span class="icon icon-link"></span></a>特殊方法</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<p>下表使用 <code>-> *</code> 代表返回值类型是任意的，或者需要视情况而定，实际上并不存在这种写法。<br>
诸如 <code>-> str</code> 仅表示绝大多数情况下应当返回 <code>str</code> 类型，或者推荐返回 <code>str</code> 类型。<br>
没有 <code>-></code> 的方法一般没有返回值。<br>
参见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html">https://docs.python.org/zh-cn/3/reference/datamodel.html</a></p>



























































<table><thead><tr><th>语句</th><th>特殊方法</th><th align="left">备注</th><th></th></tr></thead><tbody><tr><td><code>repr(obj)</code></td><td><code>__repr__(self) -> str</code></td><td align="left">详见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#repr"><code>repr()</code></a> 。</td><td></td></tr><tr><td><code>str(obj)</code></td><td><code>__str__(self) -> str</code></td><td align="left">详见 <a href="https://docs.python.org/zh-cn/3/library/stdtypes.html#str"><code>str</code> 类型</a> 。</td><td></td></tr><tr><td><code>bytes(obj)</code></td><td><code>__bytes__(self) -> bytes</code></td><td align="left">详见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#func-bytes"><code>bytes()</code></a> 。</td><td></td></tr><tr><td><code>format(obj, spec)</code></td><td><code>__format__(self, spec) -> str</code></td><td align="left">详见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#format"><code>format()</code></a>、<a href="https://docs.python.org/zh-cn/3/reference/lexical_analysis.html#f-strings">格式化字符串字面值</a>、<a href="https://docs.python.org/zh-cn/3/library/string.html#formatspec">格式规格迷你语言</a> 。</td><td></td></tr><tr><td><code>hash(obj)</code></td><td><code>__hash__(self) -> int</code></td><td align="left">详见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#hash"><code>hash()</code></a> 。</td><td></td></tr><tr><td><code>bool(obj)</code></td><td><code>__bool__(self) -> bool</code></td><td align="left">未定义时调用 <code>obj.__len__() != 0</code> ，若 <code>__len__()</code> 也未定义，则所有对象都被视为 <code>True</code> 。另见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#bool"><code>bool()</code></a> 。</td><td></td></tr><tr><td><code>dir(obj)</code></td><td><code>__dir__(self) -> list</code></td><td align="left">返回值必须是一个序列，<a href="https://docs.python.org/zh-cn/3/library/functions.html#dir"><code>dir()</code></a> 会把返回的序列转换为列表并对其排序。</td><td></td></tr><tr><td><code>Object[key]</code></td><td><code>__class_getitem__(cls, key) -> *</code></td><td align="left">不建议用于除了 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#emulating-generic-types">模拟泛型类型</a> 以外的用途，避免 IDE 误判。</td><td></td></tr></tbody></table>
<ul>
<li>自定义实例及子类检查，参见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#customizing-instance-and-subclass-checks">https://docs.python.org/zh-cn/3/reference/datamodel.html#customizing-instance-and-subclass-checks</a></li>
</ul>























<table><thead><tr><th>语句</th><th>特殊方法</th><th align="left">备注</th><th></th></tr></thead><tbody><tr><td><code>isinstance(instance, class)</code></td><td><code>class.__instancecheck__(self, instance) -> bool</code></td><td align="left">如果 instance 应被视为 class 的一个（直接或间接）实例则返回真值。</td><td></td></tr><tr><td><code>issubclass(subclass, class)</code></td><td><code>class.__subclasscheck__(self, subclass) -> bool</code></td><td align="left">如果 subclass 应被视为 class 的一个（直接或间接）子类则返回真值。</td><td></td></tr></tbody></table>
<ul>
<li>“富比较”方法，参见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#object.__lt__">https://docs.python.org/zh-cn/3/reference/datamodel.html#object.__lt__</a></li>
</ul>















































<table><thead><tr><th>语句</th><th>特殊方法</th><th align="left">备注</th><th></th></tr></thead><tbody><tr><td><code>obj &#x3C; other</code></td><td><code>__lt__(self, other) -> bool</code></td><td align="left"></td><td></td></tr><tr><td><code>obj &#x3C;= other</code></td><td><code>__le__(self, other) -> bool</code></td><td align="left"></td><td></td></tr><tr><td><code>obj == other</code></td><td><code>__eq__(self, other) -> bool</code></td><td align="left">默认返回 <code>obj is other</code> ，如果结果为 <code>False</code> ，则会返回 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#the-standard-type-hierarchy"><code>NotImplemented</code></a> 。</td><td></td></tr><tr><td><code>obj != other</code></td><td><code>__ne__(self, other) -> bool</code></td><td align="left">默认返回 <code>not obj.__eq__(other)</code> 。</td><td></td></tr><tr><td><code>obj > other</code></td><td><code>__gt__(self, other) -> bool</code></td><td align="left"></td><td></td></tr><tr><td><code>obj >= other</code></td><td><code>__ge__(self, other) -> bool</code></td><td align="left"></td><td></td></tr></tbody></table>
<ul>
<li>自定义属性访问，参见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#customizing-attribute-access">https://docs.python.org/zh-cn/3/reference/datamodel.html#customizing-attribute-access</a></li>
</ul>



































<table><thead><tr><th>语句</th><th>特殊方法</th><th align="left">备注</th><th></th></tr></thead><tbody><tr><td><code>obj.name</code></td><td><code>__getattr__(self, name) -> *</code></td><td align="left">优先调用。当抛出 <a href="https://docs.python.org/zh-cn/3/library/exceptions.html#AttributeError"><code>AttributeError</code></a> 时转向调用 <code>__getattribute__()</code> 。</td><td></td></tr><tr><td><code>obj.name</code></td><td><code>__getattribute__(self, name) -> *</code></td><td align="left">参见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#customizing-attribute-access">自定义属性访问</a> 避免无限递归。</td><td></td></tr><tr><td><code>obj.name = value</code></td><td><code>__setattr__(self, name, value)</code></td><td align="left"></td><td></td></tr><tr><td><code>del obj.name</code></td><td><code>__delattr__(self, name)</code></td><td align="left">仅在 <code>del obj.name</code> 对于该对象有意义时才应该被实现。</td><td></td></tr></tbody></table>
<ul>
<li>模拟容器类型，参见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#emulating-container-types">https://docs.python.org/zh-cn/3/reference/datamodel.html#emulating-container-types</a></li>
</ul>

































































<table><thead><tr><th>语句</th><th>特殊方法</th><th align="left">备注</th><th></th></tr></thead><tbody><tr><td><code>len(obj)</code></td><td><code>__len__(self) -> int</code></td><td align="left"></td><td></td></tr><tr><td><code>op.length_hint(obj)</code></td><td><code>__length_hint__(self) -> int</code></td><td align="left">在使用标准库 <a href="https://docs.python.org/zh-cn/3/library/operator.html">operator</a> 的 <a href="https://docs.python.org/zh-cn/3/library/operator.html#operator.length_hint"><code>length_hint()</code></a> 时会被调用（Python 3.4+）。</td><td></td></tr><tr><td><code>obj[key]</code></td><td><code>__getitem__(self, key) -> *</code></td><td align="left">需要抛出 <a href="https://docs.python.org/zh-cn/3/library/exceptions.html#IndexError">IndexError</a> 以便正确地结束 <a href="https://docs.python.org/zh-cn/3/reference/compound_stmts.html#for">for</a> 循环。</td><td></td></tr><tr><td><code>obj[key]</code></td><td><code>__missing__(self, key) -> *</code></td><td align="left">仅在 dict 的子类找不到键时被调用（不能重写 <code>__getitem__</code> 方法）。</td><td></td></tr><tr><td><code>obj[key] = value</code></td><td><code>__setitem__(self, key, value)</code></td><td align="left"><code>a[1:2] = b</code> 实际上是 <code>a[slice(1, 2, None)] = b</code> ，其它情形及在其余方法中同理。详见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#slice"><code>slice()</code></a> 。</td><td></td></tr><tr><td><code>del obj[key]</code></td><td><code>__delitem__(self, key)</code></td><td align="left"></td><td></td></tr><tr><td><em>调用途径有很多</em></td><td><code>__iter__(self) -> Iterator</code></td><td align="left">在需要创建一个 <a href="https://docs.python.org/zh-cn/3/glossary.html#term-iterator">迭代器</a> 时被调用，例如使用 <a href="https://docs.python.org/zh-cn/3/library/functions.html#iter"><code>iter()</code></a> 、 <a href="https://docs.python.org/zh-cn/3/reference/compound_stmts.html#for"><code>for</code> 循环</a> 。<br>最好返回一个新对象，因为迭代器在语义上是一次性的。若返回 <code>self</code> ，则必须实现 <code>__next__()</code> 方法。</td><td></td></tr><tr><td><code>reversed(obj)</code></td><td><code>__reversed__(self) -> *</code></td><td align="left">详见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#reversed"><code>reversed()</code></a> 。</td><td></td></tr><tr><td><code>item in obj</code></td><td><code>__contains__(self, item) -> bool</code></td><td align="left">对于未定义该方法的对象在 <code>in</code> 和 <code>not in</code> 时，参考 <a href="https://docs.python.org/zh-cn/3/reference/expressions.html#membership-test-details">成员检测运算</a> 。</td><td></td></tr></tbody></table>
<ul>
<li>模拟数字类型，参见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#emulating-numeric-types">https://docs.python.org/zh-cn/3/reference/datamodel.html#emulating-numeric-types</a></li>
</ul>



























































































































































































































































































































































<table><thead><tr><th>语句</th><th>特殊方法</th><th align="left">备注</th><th></th></tr></thead><tbody><tr><td><code>+obj</code></td><td><code>__neg__(self) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>-obj</code></td><td><code>__pos__(self) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>~obj</code></td><td><code>__invert__(self) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>abs(obj)</code></td><td><code>__abs__(self) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>int(obj)</code></td><td><code>__int__(self) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>float(obj)</code></td><td><code>__float__(self) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>complex(obj)</code></td><td><code>__complex__(self) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>round(obj)</code></td><td><code>__round__(self) -> int</code></td><td align="left">详见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#round"><code>round()</code></a> 。</td><td></td></tr><tr><td><code>round(obj)</code></td><td><code>__round__(self, ndigits) -> *</code></td><td align="left">详见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#round"><code>round()</code></a> 。</td><td></td></tr><tr><td><code>math.ceil(obj)</code></td><td><code>__ceil__(self) -> int</code></td><td align="left">详见标准库 <a href="https://docs.python.org/zh-cn/3/library/math.html#module-math">math</a> 的 <a href="https://docs.python.org/zh-cn/3/library/math.html#math.ceil"><code>ceil()</code></a> 。</td><td></td></tr><tr><td><code>math.floor(obj)</code></td><td><code>__floor__(self) -> int</code></td><td align="left">详见标准库 <a href="https://docs.python.org/zh-cn/3/library/math.html#module-math">math</a> 的 <a href="https://docs.python.org/zh-cn/3/library/math.html#math.floor"><code>floor()</code></a> 。</td><td></td></tr><tr><td><code>math.trunc(obj)</code></td><td><code>__trunc__(self) -> int</code></td><td align="left">详见标准库 <a href="https://docs.python.org/zh-cn/3/library/math.html#module-math">math</a> 的 <a href="https://docs.python.org/zh-cn/3/library/math.html#math.trunc"><code>trunc()</code></a> 。</td><td></td></tr><tr><td></td><td><code>__index__(self) -> int</code></td><td align="left">需要无损地将数值转换为整数的时候会被调用。详见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#object.__index__">这里</a> 。</td><td></td></tr><tr><td><code>obj + other</code></td><td><code>__add__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj - other</code></td><td><code>__sub__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj * other</code></td><td><code>__mul__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj @ other</code></td><td><code>__matmul__(self, other) -> *</code></td><td align="left">为第三方库而生的矩阵乘法运算符，<a href="https://docs.python.org/zh-cn/3/reference/expressions.html#binary-arithmetic-operations">这里</a>提了一嘴。（Python 3.5+）</td><td></td></tr><tr><td><code>obj / other</code></td><td><code>__truediv__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj // other</code></td><td><code>__floordiv__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj % other</code></td><td><code>__mod__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>divmod(obj, other)</code></td><td><code>__divmod__(self, other) -> tuple</code></td><td align="left"><code>divmod(a, b)</code> 返回一个元组 <code>(a // b, a % b)</code> ，详见 <a href="https://docs.python.org/zh-cn/3/library/functions.html#divmod"><code>divmod()</code></a> 。</td><td></td></tr><tr><td><code>obj ** exp</code></td><td><code>__pow__(self, exp) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>pow(obj, exp, mod)</code></td><td><code>__pow__(self, exp, mod) -> *</code></td><td align="left"><code>pow(base, exp, mod)</code> 比 <code>pow(base, exp) % mod</code> 更高效。</td><td></td></tr><tr><td><code>obj &#x3C;&#x3C; other</code></td><td><code>__lshift__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj >> other</code></td><td><code>__rshift__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj &#x26; other</code></td><td><code>__and__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj ^ other</code></td><td><code>__xor__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj | other</code></td><td><code>__or__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>other + obj</code></td><td><code>__radd__(self, obj) -> *</code></td><td align="left">仅当 obj 未定义 <code>__add__()</code> 或其返回 <code>NotImplemented</code> ，<br>且与 other 互相都没有继承关系时，调用 other 的 <code>__radd__()</code> 。详见 <a href="https://docs.python.org/zh-cn/3/reference/datamodel.html#object.__radd__">这里</a> 。</td><td></td></tr><tr><td><code>other - obj</code></td><td><code>__rsub__(self, obj) -> *</code></td><td align="left">以下，如此类推。</td><td></td></tr><tr><td><code>other * obj</code></td><td><code>__rmul__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>other @ obj</code></td><td><code>__rmatmul__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>other / obj</code></td><td><code>__rtruediv__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>other // obj</code></td><td><code>__rfloordiv__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>other % obj</code></td><td><code>__rmod__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>divmod(other, obj)</code></td><td><code>__rdivmod__(self, obj) -> tuple</code></td><td align="left"></td><td></td></tr><tr><td><code>other ** obj</code></td><td><code>__rpow__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td></td><td><code>__rpow__(self, obj, mod) -> *</code></td><td align="left"><code>pow(obj, other, mod)</code> 不会尝试调用 <code>other.__rpow__(obj, mod)</code> ，因为强制转换规则会太过复杂。</td><td></td></tr><tr><td><code>other &#x3C;&#x3C; obj</code></td><td><code>__rlshift__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>other >> obj</code></td><td><code>__rrshift__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>other &#x26; obj</code></td><td><code>__rand__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>other ^ obj</code></td><td><code>__rxor__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>other | obj</code></td><td><code>__ror__(self, obj) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj += other</code></td><td><code>__iadd__(self, other) -> *</code></td><td align="left">若方法已定义，则 <code>a += b</code> 等价于 <code>a.__iadd(b)</code> ；<br>若未定义，则回退到 <code>a + b</code> 选择 <code>x.__add__(y)</code> 和 <code>y.__radd__(x)</code> 。</td><td></td></tr><tr><td><code>obj -= other</code></td><td><code>__isub__(self, other) -> *</code></td><td align="left">以下，如此类推。</td><td></td></tr><tr><td><code>obj *= other</code></td><td><code>__imul__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj @= other</code></td><td><code>__imatmul__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj /= other</code></td><td><code>__itruediv__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj //= other</code></td><td><code>__ifloordiv__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj %= other</code></td><td><code>__imod__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj **= exp</code></td><td><code>__ipow__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj &#x3C;&#x3C;= other</code></td><td><code>__ilshift__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj >>= other</code></td><td><code>__irshift__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj &#x26;= other</code></td><td><code>__iand__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj ^= other</code></td><td><code>__ixor__(self, other) -> *</code></td><td align="left"></td><td></td></tr><tr><td><code>obj |= other</code></td><td><code>__ior__(self, other) -> *</code></td><td align="left"></td><td></td></tr></tbody></table>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="python-类型标注-python-35"><a aria-hidden="true" tabindex="-1" href="#python-类型标注-python-35"><span class="icon icon-link"></span></a>Python 类型标注 (Python 3.5+)</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="变量-1"><a aria-hidden="true" tabindex="-1" href="#变量-1"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">string<span class="token punctuation">:</span> <span class="token builtin">str</span> <span class="token operator">=</span> <span class="token string">"ha"</span>
</span><span class="code-line">times<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">3</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>string <span class="token operator">*</span> times<span class="token punctuation">)</span>  <span class="token comment"># => hahaha</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="变量-2"><a aria-hidden="true" tabindex="-1" href="#变量-2"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">result<span class="token punctuation">:</span> <span class="token builtin">str</span> <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">+</span> <span class="token number">2</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>  <span class="token comment"># => 3</span>
</span></code></pre>
<p>错误的类型标注不会影响正常运行，也不会报错</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="参数"><a aria-hidden="true" tabindex="-1" href="#参数"><span class="icon icon-link"></span></a>参数</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">say</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> start<span class="token punctuation">:</span> <span class="token builtin">str</span> <span class="token operator">=</span> <span class="token string">"Hi"</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> start <span class="token operator">+</span> <span class="token string">", "</span> <span class="token operator">+</span> name
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>say<span class="token punctuation">(</span><span class="token string">"Python"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># => Hi, Python</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="位置参数-1"><a aria-hidden="true" tabindex="-1" href="#位置参数-1"><span class="icon icon-link"></span></a>位置参数</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">calc_summary</span><span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token builtin">sum</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>calc_summary<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># => 8</span>
</span></code></pre>
<p>表示 args 的所有元素都是 int 类型的。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="返回值"><a aria-hidden="true" tabindex="-1" href="#返回值"><span class="icon icon-link"></span></a>返回值</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">say_hello</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">str</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string">"Hello, "</span> <span class="token operator">+</span> name
</span><span class="code-line">
</span><span class="code-line">var <span class="token operator">=</span> <span class="token string">"Python"</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>say_hello<span class="token punctuation">(</span>var<span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># => Hello, Python</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多种可能的返回值"><a aria-hidden="true" tabindex="-1" href="#多种可能的返回值"><span class="icon icon-link"></span></a>多种可能的返回值</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">from</span> typing <span class="token keyword">import</span> Union
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">resp200</span><span class="token punctuation">(</span>meaningful<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Union<span class="token punctuation">[</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string">"OK"</span> <span class="token keyword">if</span> meaningful <span class="token keyword">else</span> <span class="token number">200</span>
</span></code></pre>
<p>表示返回值可能是 int，也可能是 str 。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="关键字参数-1"><a aria-hidden="true" tabindex="-1" href="#关键字参数-1"><span class="icon icon-link"></span></a>关键字参数</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">calc_summary</span><span class="token punctuation">(</span><span class="token operator">**</span>kwargs<span class="token punctuation">:</span> <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token builtin">sum</span><span class="token punctuation">(</span>kwargs<span class="token punctuation">.</span>values<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>calc_summary<span class="token punctuation">(</span>a<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> b<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># => 3</span>
</span></code></pre>
<p>表示 kwargs 的所有值都是 int 类型的。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多个返回值"><a aria-hidden="true" tabindex="-1" href="#多个返回值"><span class="icon icon-link"></span></a>多个返回值</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">resp200</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token string">"OK"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多种可能的返回值-310"><a aria-hidden="true" tabindex="-1" href="#多种可能的返回值-310"><span class="icon icon-link"></span></a>多种可能的返回值 (3.10+)</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">resp200</span><span class="token punctuation">(</span>meaningful<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">int</span> <span class="token operator">|</span> <span class="token builtin">str</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string">"OK"</span> <span class="token keyword">if</span> meaningful <span class="token keyword">else</span> <span class="token number">200</span>
</span></code></pre>
<p>自 Python 3.10 起可用。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="属性"><a aria-hidden="true" tabindex="-1" href="#属性"><span class="icon icon-link"></span></a>属性</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Employee</span><span class="token punctuation">:</span>
</span><span class="code-line">    name<span class="token punctuation">:</span> <span class="token builtin">str</span>
</span><span class="code-line">    age<span class="token punctuation">:</span> <span class="token builtin">int</span>
</span><span class="code-line">
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> name<span class="token punctuation">,</span> age<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">        self<span class="token punctuation">.</span>name <span class="token operator">=</span> name
</span><span class="code-line">        self<span class="token punctuation">.</span>age <span class="token operator">=</span> age
</span><span class="code-line">        self<span class="token punctuation">.</span>graduated<span class="token punctuation">:</span> <span class="token builtin">bool</span> <span class="token operator">=</span> <span class="token boolean">False</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="标注自己"><a aria-hidden="true" tabindex="-1" href="#标注自己"><span class="icon icon-link"></span></a>标注自己</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Employee</span><span class="token punctuation">:</span>
</span><span class="code-line">    name<span class="token punctuation">:</span> <span class="token builtin">str</span>
</span><span class="code-line">    age<span class="token punctuation">:</span> <span class="token builtin">int</span>
</span><span class="code-line">
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">set_name</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> name<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token string">"Employee"</span><span class="token punctuation">:</span>
</span><span class="code-line">        self<span class="token punctuation">.</span>name <span class="token operator">=</span> name
</span><span class="code-line">        <span class="token keyword">return</span> self
</span></code></pre>
<p>这里表示 set_name() 返回了一个 Employee 对象。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="标注自己-311"><a aria-hidden="true" tabindex="-1" href="#标注自己-311"><span class="icon icon-link"></span></a>标注自己 (3.11+)</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">from</span> typing <span class="token keyword">import</span> Self
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Employee</span><span class="token punctuation">:</span>
</span><span class="code-line">    name<span class="token punctuation">:</span> <span class="token builtin">str</span>
</span><span class="code-line">    age<span class="token punctuation">:</span> <span class="token builtin">int</span>
</span><span class="code-line">
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token function">set_name</span><span class="token punctuation">(</span>self<span class="token punctuation">:</span> Self<span class="token punctuation">,</span> name<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Self<span class="token punctuation">:</span>
</span><span class="code-line">        self<span class="token punctuation">.</span>name <span class="token operator">=</span> name
</span><span class="code-line">        <span class="token keyword">return</span> self
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="标注一个值为类型的参数"><a aria-hidden="true" tabindex="-1" href="#标注一个值为类型的参数"><span class="icon icon-link"></span></a>标注一个值为类型的参数</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">from</span> typing <span class="token keyword">import</span> TypeVar<span class="token punctuation">,</span> Type
</span><span class="code-line">
</span><span class="code-line">T <span class="token operator">=</span> TypeVar<span class="token punctuation">(</span><span class="token string">"T"</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># "mapper" 的值是一个像 int、str、MyClass 这样的类型</span>
</span><span class="code-line"><span class="token comment"># "default" 是一个 T 类型的值，比如 314、"string"、MyClass()</span>
</span><span class="code-line"><span class="token comment"># 函数的返回值也是一个 T 类型的值</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">converter</span><span class="token punctuation">(</span>raw<span class="token punctuation">,</span> mapper<span class="token punctuation">:</span> Type<span class="token punctuation">[</span>T<span class="token punctuation">]</span><span class="token punctuation">,</span> default<span class="token punctuation">:</span> T<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> T<span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">try</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">return</span> mapper<span class="token punctuation">(</span>raw<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">except</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">return</span> default
</span><span class="code-line">
</span><span class="code-line">raw<span class="token punctuation">:</span> <span class="token builtin">str</span> <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">"请输入一个整数："</span><span class="token punctuation">)</span>
</span><span class="code-line">result<span class="token punctuation">:</span> <span class="token builtin">int</span> <span class="token operator">=</span> converter<span class="token punctuation">(</span>raw<span class="token punctuation">,</span> mapper<span class="token operator">=</span><span class="token builtin">int</span><span class="token punctuation">,</span> default<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="标注一个值为函数的参数"><a aria-hidden="true" tabindex="-1" href="#标注一个值为函数的参数"><span class="icon icon-link"></span></a>标注一个值为函数的参数</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">from</span> typing <span class="token keyword">import</span> TypeVar<span class="token punctuation">,</span> Callable<span class="token punctuation">,</span> Any
</span><span class="code-line">
</span><span class="code-line">T <span class="token operator">=</span> TypeVar<span class="token punctuation">(</span><span class="token string">"T"</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">converter</span><span class="token punctuation">(</span>raw<span class="token punctuation">,</span> mapper<span class="token punctuation">:</span> Callable<span class="token punctuation">[</span><span class="token punctuation">[</span>Any<span class="token punctuation">]</span><span class="token punctuation">,</span> T<span class="token punctuation">]</span><span class="token punctuation">,</span> default<span class="token punctuation">:</span> T<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> T<span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">try</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">return</span> mapper<span class="token punctuation">(</span>raw<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">except</span><span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">return</span> default
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># Callable[[Any], T] 表示值是一个像这样声明的函数:</span>
</span><span class="code-line"><span class="token comment"># def anynomous(arg: Any) -> T:</span>
</span><span class="code-line"><span class="token comment">#     pass</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">def</span> <span class="token function">is_success</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token builtin">bool</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">return</span> value <span class="token keyword">in</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"OK"</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token string">"success"</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">resp <span class="token operator">=</span> <span class="token builtin">dict</span><span class="token punctuation">(</span>code<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> message<span class="token operator">=</span><span class="token string">"OK"</span><span class="token punctuation">,</span> data<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">successed<span class="token punctuation">:</span> <span class="token builtin">bool</span> <span class="token operator">=</span> converter<span class="token punctuation">(</span>resp<span class="token punctuation">.</span>message<span class="token punctuation">,</span> mapper<span class="token operator">=</span>is_success<span class="token punctuation">,</span> default<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="各种各样的"><a aria-hidden="true" tabindex="-1" href="#各种各样的"><span class="icon icon-link"></span></a>各种各样的</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="注释"><a aria-hidden="true" tabindex="-1" href="#注释"><span class="icon icon-link"></span></a>注释</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token comment"># 这是单行注释</span>
</span></code></pre>
<hr>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token triple-quoted-string string">""" 可以写多行字符串
</span></span><span class="code-line"><span class="token triple-quoted-string string">    使用三个"，并且经常使用
</span></span><span class="code-line"><span class="token triple-quoted-string string">    作为文档。
</span></span><span class="code-line"><span class="token triple-quoted-string string">"""</span>
</span></code></pre>
<hr>
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token triple-quoted-string string">''' 可以写多行字符串
</span></span><span class="code-line"><span class="token triple-quoted-string string">    使用三个'，并且经常使用
</span></span><span class="code-line"><span class="token triple-quoted-string string">    作为文档。
</span></span><span class="code-line"><span class="token triple-quoted-string string">'''</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="生成器"><a aria-hidden="true" tabindex="-1" href="#生成器"><span class="icon icon-link"></span></a>生成器</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token function">double_numbers</span><span class="token punctuation">(</span>iterable<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">for</span> i <span class="token keyword">in</span> iterable<span class="token punctuation">:</span>
</span><span class="code-line">        <span class="token keyword">yield</span> i <span class="token operator">+</span> i
</span></code></pre>
<p>生成器可帮助您编写惰性代码</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="要列出的生成器"><a aria-hidden="true" tabindex="-1" href="#要列出的生成器"><span class="icon icon-link"></span></a>要列出的生成器</h3><div class="wrap-body">
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line">values <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">-</span>x <span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">gen_to_list <span class="token operator">=</span> <span class="token builtin">list</span><span class="token punctuation">(</span>values<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># => [-1, -2, -3, -4, -5]</span>
</span><span class="code-line"><span class="token keyword">print</span><span class="token punctuation">(</span>gen_to_list<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="处理异常"><a aria-hidden="true" tabindex="-1" href="#处理异常"><span class="icon icon-link"></span></a>处理异常</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token keyword">try</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token comment"># 使用“raise”来引发错误</span>
</span><span class="code-line">    <span class="token keyword">raise</span> IndexError<span class="token punctuation">(</span><span class="token string">"这是一个索引错误"</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">except</span> IndexError <span class="token keyword">as</span> e<span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">pass</span>                 <span class="token comment"># pass只是一个空操作。 通常你会在这里做恢复。</span>
</span><span class="code-line"><span class="token keyword">except</span> <span class="token punctuation">(</span>TypeError<span class="token punctuation">,</span> NameError<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token keyword">pass</span>                 <span class="token comment"># 如果需要，可以一起处理多个异常。</span>
</span><span class="code-line"><span class="token keyword">else</span><span class="token punctuation">:</span>                    <span class="token comment"># try/except 块的可选子句。 必须遵循除块之外的所有内容</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"All good!"</span><span class="token punctuation">)</span>   <span class="token comment"># 仅当 try 中的代码未引发异常时运行</span>
</span><span class="code-line"><span class="token keyword">finally</span><span class="token punctuation">:</span>                 <span class="token comment"># 在所有情况下执行</span>
</span><span class="code-line">    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"我们可以在这里清理资源"</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="pyenv--pipenv"><a aria-hidden="true" tabindex="-1" href="#pyenv--pipenv"><span class="icon icon-link"></span></a>pyenv &#x26; pipenv</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<p>pvenv 用于管理python版本，pipenv 用于管理项目包版本</p>
<h4 id="pyenv"><a aria-hidden="true" tabindex="-1" href="#pyenv"><span class="icon icon-link"></span></a>pyenv</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 安装 pyenv</span>
</span><span class="code-line"><span class="token function">curl</span> https://pyenv.run <span class="token operator">|</span> <span class="token function">bash</span>
</span></code></pre>
<p><a href="https://github.com/pyenv/pyenv#installation">更多安装方式</a></p>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 安装 python 版本</span>
</span><span class="code-line">pyenv <span class="token function">install</span> <span class="token number">3.10</span>.12
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 设置 python 版本</span>
</span><span class="code-line">pyenv global <span class="token number">3.10</span>.12 <span class="token comment"># 全局设置</span>
</span><span class="code-line">pyenv shell  <span class="token number">3.10</span>.12 <span class="token comment"># 针对当前 shell session</span>
</span><span class="code-line">pyenv <span class="token builtin class-name">local</span>  <span class="token number">3.10</span>.12 <span class="token comment"># 针对当前目录 </span>
</span></code></pre>
<h4 id="pipenv"><a aria-hidden="true" tabindex="-1" href="#pipenv"><span class="icon icon-link"></span></a>pipenv</h4>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 安装 pipenv</span>
</span><span class="code-line">pip <span class="token function">install</span> pipenv <span class="token parameter variable">--user</span>  <span class="token comment"># pip</span>
</span><span class="code-line">brew <span class="token function">install</span> pipenv        <span class="token comment"># homebrew</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 更新 pipenv</span>
</span><span class="code-line">pip <span class="token function">install</span> <span class="token parameter variable">--user</span> <span class="token parameter variable">--upgrade</span> pipenv <span class="token comment"># pip</span>
</span><span class="code-line">brew upgrade pipenv                 <span class="token comment"># homebrew</span>
</span></code></pre>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 将 pipenv 命令加入到系统环境变量 $PATH 中 (Unix and MacOS)</span>
</span><span class="code-line"><span class="token assign-left variable">dir</span><span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span>python <span class="token parameter variable">-c</span> <span class="token string">'import site; print(site.USER_BASE + "/bin")'</span><span class="token variable">)</span></span> <span class="token comment"># 打印 python site-packages bin 路径</span>
</span><span class="code-line"><span class="token builtin class-name">echo</span> <span class="token string">'export PATH="'</span><span class="token variable">$dir</span><span class="token string">':$PATH"'</span> <span class="token operator">>></span> ~/.zshrc <span class="token comment"># 将 dir 路径加入到 PATH 中</span>
</span><span class="code-line"><span class="token builtin class-name">source</span> ~/.zshrc
</span><span class="code-line">
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 安装 package</span>
</span><span class="code-line">pipenv <span class="token function">install</span> <span class="token operator">&#x3C;</span>package name<span class="token operator">></span> <span class="token comment"># 不指定版本</span>
</span><span class="code-line">pipenv <span class="token function">install</span> <span class="token operator">&#x3C;</span>package name<span class="token operator">>=</span><span class="token operator">=</span><span class="token operator">&#x3C;</span>version<span class="token operator">></span>    <span class="token comment"># 精确指定版本</span>
</span><span class="code-line">pipenv <span class="token function">install</span> <span class="token operator">&#x3C;</span>package name<span class="token operator">></span>~<span class="token operator">=</span><span class="token operator">&#x3C;</span>version<span class="token operator">></span>    <span class="token comment"># 指定版本范围，例如 1.1则表示安装1.x的最新版本，1.0.1则表示安装1.0.x的最新版本</span>
</span><span class="code-line">pipenv <span class="token function">install</span> <span class="token string">"&#x3C;package name>=&#x3C;version>"</span>   <span class="token comment"># 大于等于指定版本</span>
</span><span class="code-line">pipenv <span class="token function">install</span> <span class="token string">"&#x3C;package name>=&#x3C;version>"</span>   <span class="token comment"># 小于等于指定版本</span>
</span></code></pre>
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line"><span class="token comment"># 指定 python 版本</span>
</span><span class="code-line">pipenv <span class="token parameter variable">--python</span> <span class="token number">3.10</span>.12
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 激活当前目录虚拟环境</span>
</span><span class="code-line">pipenv shell
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://www.python.org/">Python 官方网站</a>  <em>(python.org)</em></li>
<li><a href="https://docs.python.org/zh-cn/3/index.html">Python 文档</a>  <em>(docs.python.org)</em></li>
<li><a href="https://learnxinyminutes.com/docs/zh-cn/python-cn/">Y 分钟学会 Python</a> <em>(learnxinyminutes.com)</em></li>
<li><a href="./regex.html#python-%E4%B8%AD%E7%9A%84%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F">Python 中的正则表达式</a> <em>(jaywcjlove.github.io)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 <a href="https://wangchujiang.com/#/app" target="_blank">Kenny Wang</a>.</footer></footer><script src="../data.js?v=1.5.5" defer></script><script src="../js/fuse.min.js?v=1.5.5" defer></script><script src="../js/main.js?v=1.5.5" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
